How can I write a script to calculate the time the script took to complete?
I thought this would be the case, but obviously not..
@echo off
set starttime=%time%
set endtime=%time%
REM do stuff here
set /a runtime=%endtime%-%starttime%
echo Script took %runtime% to complete
This is kind of tangential, but it may help you out.
Microsoft has a timeit.exe program that works more or less like an enhanced version of the unix 'time' command. It comes in the Windows Server 2003 Resource Kit, and it has pretty much replaced all the times I've wanted to do something like what you're suggesting.
It may be worth a look.
Excellent little routine. However, if the calculation spans across two days, the calculation is incorrect. The result needs subtracting from 24 hours (8640000 centiseconds).
Also remove one of the duplicate 'set' commands in the relevant line.
Note that regional settings affect the format of the TIME function, the decimal being a fullstop in UK rather than a comma.
The lines
rem we might have measured the time inbetween days
need changing to
rem we might have measured the time across days
You can't do time arithmetic directly in batch scripting, so you'll either need an external program to calculate the time difference, or extract each part of the time and do arithmetic that way.
Take a look at the bottom of this forum thread for an example of how to do the latter. Excerpted here:
I also assume it's a typo, but you do want to be sure to set
endtime
after processing.My own personal preference is to install Cygwin and use the
time
command but, if you actually have to do it as a batch file, you can't just subtract the strings, you have to treat them as parts.The following script will time a 10-second ping command with the ability to cross a single day boundary without getting tied up in negative numbers. A slight enhancement would allow it to cross many day boundaries.
Check out this script that will retrieve time through WMI so it's Regional Setting independent.
Two things leap out about the original batch file.
but neither is going to help in the long run.Whatever your benchmark method, be sure to capture the start time before the operation, and the end time after the operation. Your sample doesn't do that.
%time%
evaluates to something like "12:34:56.78", and theSET /A
command can't subtract those. You need a command that produces a simple scalar time stamp.I was going to say it can't be done, but the batch language is a lot more powerful than it is given credit for, so here is a simple implementation of TIMER.BAT. For the record, Pax beat me to an answer showing the string splitting while I was fiddling around, and Johannes Rössel suggested moving the arithmetic outside of the measured region:
You could simplify the arithmetic and be a little more honest about the overall accuracy of this by not bothering with the 100ths of a second part. Here it is in action, assuming you have a sleep command or some other time waster:
Edit: I revised the code and its description as suggested in a comment.
I think that when the NT team replaced COMMAND.COM with CMD.EXE, they thought they wouldn't get away with making it very different. But in effect, it is almost an entirely new language. Many of the old favorite commands have new features if the extensions are enabled.
One of those is
SETLOCAL
which prevents variables from modifying the caller's environment. Another isSET /A
which gives you a remarkable amount of arithmetic. The principle trick I've used here is the new substring extraction syntax where%t:~3,2%
means the two characters starting at offset 3 in the value of the variable namedt
.For a real shocker, take a look at the full description of set (try
SET /?
at a prompt) and if that doesn't scare you, look atFOR /?
and notice that it can parse text out of files...Edit 2: Fixed mis-handling of time fields containing
08
or09
reported by Frankie in comments. Tweaked a couple of things, and added some comments.Note that there is a glaring oversight here that I'm probably not going to fix. It won't work if the command starts on a different day than it ends. That is, it will do some math related to the time of day and report a difference, but the difference won't mean much.
Fixing it to at least warn about this case is easy. Fixing it to do the right thing is harder.
Edit 3: Fixed error where the %h% is not set properly for single digit hours. This is due to %time% returning " 9:01:23.45". Notice the space. Using %time: =0% replaces the space with a leading zero and %h% will be set correctly. This error only occurred when a script ran from one single digit hour to the next.