Windows equivalent to UNIX "time" command
So, Unix has a time
command that lets users time their code/other things. I was wondering if the Windows command line has anything similar.
Also, I asked a previous question regarding the Linux command line here. Can we do the same for Windows? If so, how?
Use Powershell
Measure-Command {start-process whateveryouwantexecute -Wait}
Edited to your need @efficiencylsBliss:
Measure-Command {start-process java -argumentlist "whateverargumentisneeded" -wait}
I'm using Win XP, for some reason, timeit.exe
is not working for me. I found another alternative: ptime
:
ptime 1.0 - Accurately measure program execution time
ptime will run any specified command and parameters, and measure the execution time (run time) in seconds, accurate to 5 millisecond or better. It is an automatic process timer, or program timer used for benchmark purposes.
You can cheat a little with a batch script...
@echo off
echo %time% < nul
cmd /c %1
echo %time% < nul
Then run your program as an argument to this script...
timer myprogram.exe
and for arguments...
timer "myprogram.exe -sw1 -sw2"
example output:
17:59:20.02
some text
17:59:20.03
place the batch script somewhere in your PATH variable, e.g. C:\Windows\System32
and name it timer.cmd
. Of course there is a small performance hit of forking a second cmd instance, although very minimal.
For ease of use, here is the chocolatey package:
https://chocolatey.org/packages/ptime
C:/> choco install ptime
The advantage if ptime
is that it acts like the unix version and yields the console output, which Measure-Command { XXX }
does not (or at least I don't know how to do that).
Some coffee helped me come up with this:
function time { $Command = "$args"; Measure-Command { Invoke-Expression $Command 2>&1 | out-default} }
And if you want it to output nothing, just replace with out-null:
function timequiet { $Command = "$args"; Measure-Command { Invoke-Expression $Command 2>&1 | out-null} }
You use it like this:
PS C:\> time sleep 5
Days : 0
Hours : 0
Minutes : 0
Seconds : 4
Milliseconds : 990
Ticks : 49906722
TotalDays : 5,77624097222222E-05
TotalHours : 0,00138629783333333
TotalMinutes : 0,08317787
TotalSeconds : 4,9906722
TotalMilliseconds : 4990,6722
PS C:\>