Accurately measure execution time in Windows 7
While UNIX has a shell command to measure execution time (it’s called time, and it’s been there rock-steady since 1990), Windows by default doesn’t. The right answer to the question “how do I accurately measure execution time in Windows XP’ used to be ‘use the timeit command from the Windows Server 2003 Resource Kit‘.
Unfortunately as time passes and Windows versions come and go this answer does not apply anymore to Windows 7, because the timeit command from 2003 is unsupported and does not work in Windows 7. And although there is Windows 7 Resource Kit book, the unsupported Resource Kit tools are no longer provided.
The new answer is ‘use the Windows PowerShell Measure-Command’. The matter gets more complex if you happen to have legacy batch files that rely on the timeit command. One option would be to port everything from batch files to PowerShell scripts, since PowerShell is so much better (and finally brings the Windows programmer to the same level of 1990 UNIX shell scripts).
But .. but .. if you don’t want to switch to PowerShell today, here is a recipy to patch those batch files.
Create a batch file timeit.bat:
powershell timeit.ps1 %1 %2 %3<br />
Create a PowerShell script timeit.ps1:
Param (<br /> [string] $command,<br /> [string] $arg1,<br /> [string] $arg2,<br /> [string] $arg3<br /> )<br /> if ($arg1 -eq "") { Measure-Command { Start-Process -Wait -NoNewWindow $command } }<br /> elseif ($arg2 -eq "") { Measure-Command { Start-Process -Wait -NoNewWindow $command $arg1 } }<br /> elseif ($arg3 -eq "") { Measure-Command { Start-Process -Wait -NoNewWindow -FilePath $command $arg1, $arg2 } }<br /> else { Measure-Command { Start-Process -Wait -NoNewWindow $command $arg1, $arg2, $arg3 } }<br />
Finally enable execution of unsigned PowerShell scripts written by yourself, by opening a PowerShell with administrative rights (Start+powershell, RMB click on Windows PowerShell, “Launch as administrator”) and type:
Set-ExecutionPolicy RemoteSigned<br />
That’s it. There will be surprising path handling changes. The timing output will come out in stdout as opposed to stderr where you’d get it with timeit. But at least you can stick to your beloved batch files !