Print Batch Time in Milliseconds

2020-07-23 07:09发布

问题:

I am aware that one can print batch time up to the centisecond with @echo %time%. Is there a similar command to get milliseconds as well?

回答1:

There is a resource kit utility named Timethis that provides up to the millisecond time measurements :

TimeThis :  Command Line :  dir
TimeThis :    Start Time :  Wed Oct 24 12:49:56 2012
TimeThis :      End Time :  Wed Oct 24 12:49:56 2012
TimeThis :  Elapsed Time :  00:00:00.093


回答2:

(Edit: doh, didn't notice that this has the windows tag. Leaving the answer here in case anyone else clicked on this for Linux)

If you are asking to time the execution length of a program or script, precede the command with time, for example:

time ls -R

and this will give you execution time in milliseconds:

real    0m0.667s
user    0m0.000s
sys     0m0.144s

If you are asking for the current date and time:

Take a look at:

man date

For example,

date +"%X %N"

This will give you the current time in Hour:Minutes:Seconds and then the Nanoseconds

Hope this answers your question



回答3:

Gnu gdate can make this very simple; try the code below in a batch file, for instance. I use it to create unique filenames for batch processes invoked in command windows, and on my system under Windows 7 it's evident that, at least in a command window, two immediately successive uses of gdate never give identical results.

gdate +"%%Y%%m%%d%%H%%M%%S%%N"
gdate +"%%Y%%m%%d%%H%%M%%S%%N"

set i=
set T=
for /f %%i in ('gdate +%%Y%%m%%d%%H%%M%%S%%N') do set UniqueTime=%%i

echo %UniqueTime%

But I actually use something more like the line below, discarding the last five digits because for my purposes they never seem to matter: the preceding part is always unique. This lets the unique file names be shorter.

gdate +"%%Y%%m%%d%%H%%M%%S%%N" | sed "s/.....$//"

Pretty obviously, the same expression works under Linux, so one can also use it there.



回答4:

ptime utility is "5ms or better" accurate

ptime echo hallo

ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
Copyright(C) 2002, Jem Berkes <jberkes@pc-tools.net>

===  echo hallo ===
hallo

Execution time: 0.022 s


回答5:

Use wmic os get LocalDateTime. That's also the reliable way to get locale-independent date and time

for /F "usebackq tokens=2 delims==" %%i in (
    `wmic os get LocalDateTime /VALUE`
) do @set ctime=%%i
echo milliseconds: %ctime:~15,3%