Поделиться через


Measuring How Long Commands Take in Windows

I was considering installing the timeit.exe command on my new laptop, but read that it may not work on the latest versions of Windows.  So, I found an article on StackOverflow that recommended using the Measure-Command command in PowerShell.  You use it as follows:

PS C:\> Measure-Command { Get-ChildItem }

Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 28
Ticks : 288474
TotalDays : 3.33881944444444E-07
TotalHours : 8.01316666666667E-06
TotalMinutes : 0.00048079
TotalSeconds : 0.0288474
TotalMilliseconds : 28.8474

If you just want to see one of these results, you can specify the field, such as milliseconds, as follows:

PS C:\> (Measure-Command { Get-EventLog "windows powershell" }).Milliseconds

305

Note that Measure-Command does not show you the output of the command that you have executed for PowerShell commands, just the time it took to execute the command.  If you want to see the command's output, pipe it to Out-Default, as follows:

PS C:\> Measure-Command { Get-EventLog "windows powershell" | Out-Default }

If you want to use it to execute cmd.exe commands vs. PowerShell commands, use it as follows:

PS C:\> Measure-Command { cmd /c dir /s c:\windows > nul }

Thanks to Casey K and TechGibbon for the useful information.

Rob