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?

link|improve this question

57% accept rate
Windows has the standard cmd.exe but if you want something closer to the linux version get the powershell, its way better. – Guillermo Siliceo Trueba Jan 1 '11 at 22:53
feedback

7 Answers

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.

link|improve this answer
that prompt wouldn't work, @echo off echo %time% < nul cmd /c %1 echo %time% < nul you left out the closing % on the environment variable(?) – user61885 Jan 7 '11 at 18:07
There is no closing % for command line parameters like %1. Is that what you meant? – Andrew J. Brehm Apr 30 '11 at 11:42
feedback

Use Powershell

Measure-Command {start-process whateveryouwantexecute -Wait}

Edited to your need @efficiencylsBliss:

Measure-Command {start-process java -argumentlist "whateverargumentisneeded" -wait}
link|improve this answer
Here's what I tried: Measure-Command {java test <inputfile> -Wait}. I got a list of time-related statistics, but not the output from the code. Am I doing something wrong? – efficiencyIsBliss Jan 2 '11 at 0:50
@efficiency: You forgot the start-process command. – Sasha Chedygov Jan 2 '11 at 1:40
Start-Process : A positional parameter cannot be found that accepts argument '.\6-8.txt'. At line:1 char:31 + Measure-Command {start-process <<<< java .\dancebattle .\6-8.txt -wait} + CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand` – efficiencyIsBliss Jan 2 '11 at 15:01
The above is what I got when I tried the command on a file named dancebattle that took the file 6-8.txt as input. – efficiencyIsBliss Jan 2 '11 at 15:03
@efficiencylsBliss i add an example suited to your need, don't forget that you can read about any cmdlet trough the command "man" (which in fact is an alias to get-help) – voodoomsr Jan 2 '11 at 19:53
feedback

I searched for "command time for windows" on google on the first result gives:

http://stackoverflow.com/questions/673523/how-to-measure-execution-time-of-command-in-windows-command-line

link|improve this answer
I'm on Windows 7 and I just tried the solution posted above. I get the following error: Unable to query system performance data (c0000004). I googled it and someone else had the exact same problem, but the forum suggests no solution. Thanks for the suggestion anyway. Can someone suggest something for the other part? – efficiencyIsBliss Jan 1 '11 at 23:10
1  
@eff, timeit.exe is for server 2003 and XP AFAIK, I don't think its compatible with 7. – John T Jan 1 '11 at 23:39
feedback

There is no direct equivalent to Unix time on Windows.

The University of Georgia have a brief list of Windows commands for Unix users

I find the older Windows command prompt and .bat scripting is rather limited compared to Unix shells but there are some facilities for looping over files etc. CommandWindows.com has some tips

You could either install bash on Windows (e.g. by installing CygWin) or learn Windows Powershell (which I am assuming has a means of doing something equivalent).

link|improve this answer
feedback

The output for your code can be piped to a file: java test <inputfile> | Out-File d:\a.txt

For measuring how long it takes you have to encapsulate it in Measure-Commmand:

Measure-Commmand {java test <inputfile> | Out-File d:\a.txt}

link|improve this answer
feedback

If you try to use PowerShell with Measure-Command be aware that there may be some unexpected gotchas. My command writes binary data to a file using > redirection but PowerShell added a BOM to the beginning of the file and a CRLF line break after every write!

link|improve this answer
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.