views:

4608

answers:

3

How do I print the time (in ms) in a Windows batch file?

I want to measure the time that passes between lines in my batch file, but Windows's "time /T" does not print milliseconds.

Is there a tiny 3rd party command line utility that does this perhaps?

echo %time% won't help because it's evaluated just once and thus prints the same time every time.

+1  A: 

Maybe this tool could help? It doesn't return the time, but it is a good tool to measure the time a command takes.

Stefan
Thanks, +1 helpful. But still interesting why there's no simple time print in batch...
Assaf Lavie
+2  A: 

%time% should work, provided enough time has elapsed between calls:

@echo OFF

@echo %time%
ping -n 1 -w 1 127.0.0.1 1>nul
@echo %time%

On my system I get the following output:

6:46:13.50
6:46:13.60

Patrick Cuff
On my machine it shows the same time even if I sleep between calls.
Assaf Lavie
Can you post the code for the batch file that you're trying to profile?
Patrick Cuff
nm, I'm an idiot. :)
Assaf Lavie
I doubt that :) What was the issue? Would sharing it here help others who run into the same thing?
Patrick Cuff
A: 

If you're doing something like

for /l %%i in (1,1,500) do @echo %time%

or

if foo (
    echo %time%
    do_something
    echo %time%
)

then you could simply put a setlocal enabledelayedexpansion at the beginning of your batch file and use !time! instead of %time% which gets evaluated on execution, not on parsing the line (which includes complete blocks enclosed in parentheses).

Joey