Is there a built-in way to measure execution time of a command on the Windows command line?
In computing, TIME is a command in DEC RT-11, DOS, IBM OS/2, Microsoft Windows and a number of other operating systems that is used to display and set the current system time. It is included in command-line interpreters (shells) such as COMMAND.COM , cmd.exe , 4DOS, 4OS2 and 4NT.
Display or set the system time. TIME /T Key new_time : The time as HH:MM TIME with no parameters will display the current time and prompt for a new value. Pressing ENTER will keep the same time. /T : Just display the time, formatted according to the current Regional settings.
TIME. TIME hh:mm[:ss][.cc][A|P] Purpose: Displays current time setting of system clock and provides a way for you to reset the time. Discussion. You can enter the TIME command alone and the program will prompt you when to enter the time.
PowerShell has a cmdlet for this called Measure-Command
. You'll have to ensure that PowerShell is available on the machine that runs it.
PS> Measure-Command { echo hi } Days : 0 Hours : 0 Minutes : 0 Seconds : 0 Milliseconds : 0 Ticks : 1318 TotalDays : 1.52546296296296E-09 TotalHours : 3.66111111111111E-08 TotalMinutes : 2.19666666666667E-06 TotalSeconds : 0.0001318 TotalMilliseconds : 0.1318
Measure-Command
captures the command's output. You can redirect the output back to your console using Out-Default
:
PS> Measure-Command { echo hi | Out-Default } hi Days : 0 ...
As Makotoe commented, Measure-Command
returns a TimeSpan
object, so the measured time is printed as a bunch of fields. You can format the object into a timestamp string using ToString()
:
PS> (Measure-Command { echo hi | Out-Default }).ToString() hi 00:00:00.0001318
If the command inside Measure-Command
changes your console text color, use [Console]::ResetColor()
to reset it back to normal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With