Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Can this be done with standard means?

share|improve this question
What do you mean with "standard means"? – eKek0 Mar 23 '09 at 14:27
14  
I assume "without installing additional software", therefore able to work on (nearly) all Windows systems without problems. – Јοеу Mar 23 '09 at 18:27
Johannes is right. – Kuroki Kaze Mar 24 '09 at 9:31

17 Answers

up vote 57 down vote accepted

The Windows Server 2003 Resource Kit contains timeit.exe that displays detailed execution stats. Here is an example, timing the command "timeit -?":

C:\>timeit timeit -?
Invalid switch -?
Usage: TIMEIT [-f filename] [-a] [-c] [-i] [-d] [-s] [-t] [-k keyname | -r keyname] [-m mask] [commandline...]
where:        -f specifies the name of the database file where TIMEIT
                 keeps a history of previous timings.  Default is .\timeit.dat
              -k specifies the keyname to use for this timing run
              -r specifies the keyname to remove from the database.  If
                 keyname is followed by a comma and a number then it will
                 remove the slowest (positive number) or fastest (negative)
                 times for that keyname.
              -a specifies that timeit should display average of all timings
                 for the specified key.
              -i specifies to ignore non-zero return codes from program
              -d specifies to show detail for average
              -s specifies to suppress system wide counters
              -t specifies to tabular output
              -c specifies to force a resort of the data base
              -m specifies the processor affinity mask

Version Number:   Windows NT 5.2 (Build 3790)
Exit Time:        7:38 am, Wednesday, April 15 2009
Elapsed Time:     0:00:00.000
Process Time:     0:00:00.015
System Calls:     731
Context Switches: 299
Page Faults:      515
Bytes Read:       0
Bytes Written:    0
Bytes Other:      298

You can get TimeIt in the Windows 2003 Resource Kit. Download it here.

share|improve this answer
Thanks, that's what i need :) – Kuroki Kaze Apr 17 '09 at 9:13
4  
This kit has issues with windows 2008 64bit and does not work on 2008 R2 – Artem Nov 12 '09 at 2:29
I don't need it to work in Win 2008 :) – Kuroki Kaze Dec 18 '09 at 9:46
is there any way to also measure kernel time and/or the time used by spawned sub processes? – rogerdpack Feb 21 '11 at 22:55
3  
FYI - I don't think timeit works on Windows 7 64-bit. You get the error "Unable to query system performance data (c0000004). Instead, use PowerShell's "Measure-Command" like Casey.K suggests: stackoverflow.com/questions/673523/… – Michael La Voie Oct 25 '11 at 0:05

Alternatively, Windows PowerShell has a built in command that is similar to bash's "time" command. It is called "Measure-Command." You'll have to ensure that PowerShell is installed on the machine that runs it.

Example Input:

Measure-Command {echo hi}

Example Output:

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
share|improve this answer
2  
This is the best solution for Windows 7 users as timeit.exe doesn't appear to support Windows 7 – Michael La Voie Oct 25 '11 at 0:08
7  
In case you want to use internal dos commands (eg.: dir, echo, del, etc.) don't forget to insert "cmd /c": Measure-Command { cmd /c dir /s c:\windows > nul } – TechGibbon Jan 10 '12 at 10:49

Hehe, the most simple solution might be this:

echo %time%
YourApp.exe
echo %time%

This works on every Windows out of the box.

share|improve this answer
works, but doesn't tell you cpu time used :) – rogerdpack Feb 21 '11 at 21:13
27  
Are you crazy?! That involves doing math, like, in your brain. This is why we invented computers, so we could avoid doing stuff like that. – Luke Sampson Jun 2 '11 at 1:31
brilliant! Even though, for CPU time it doesn't really help – Elijah Saounkine Jun 10 '11 at 6:55
4  
Even better (if your app spits output to the console): set startTime=%time% \n YourApp.exe \n echo Start Time: %startTime% \n echo Finish Time: %time% (Sorry, couldn't get newlines in the comment) – Jono Jun 3 '12 at 15:52
@LukeSampson, you can use set to do the math. ;-) Oh wait, nevermind, you already did that below. – Synetech Apr 21 at 14:56

If you want

  1. To measure execution time down to the hundredth of a second in (hh:mm:ss.ff format)
  2. To not have to download and install a resource pack
  3. To look like a huge DOS nerd (who doesn't)

Try copying the following script into a new batch file (e.g. timecmd.bat):

EDIT: added improvements suggested by @ScottStafford

@echo off
@setlocal 

set start=%time%

:: runs your command
cmd /c %*

set end=%time%
set options="tokens=1-4 delims=:."
for /f %options% %%a in ("%start%") do set start_h=%%a&set /a start_m=100%%b %% 100&set /a start_s=100%%c %% 100&set /a start_ms=100%%d %% 100
for /f %options% %%a in ("%end%") do set end_h=%%a&set /a end_m=100%%b %% 100&set /a end_s=100%%c %% 100&set /a end_ms=100%%d %% 100

set /a hours=%end_h%-%start_h%
set /a mins=%end_m%-%start_m%
set /a secs=%end_s%-%start_s%
set /a ms=%end_ms%-%start_ms%
if %hours% lss 0 set /a hours = 24%hours%
if %mins% lss 0 set /a hours = %hours% - 1 & set /a mins = 60%mins%
if %secs% lss 0 set /a mins = %mins% - 1 & set /a secs = 60%secs%
if %ms% lss 0 set /a secs = %secs% - 1 & set /a ms = 100%ms%
if 1%ms% lss 100 set ms=0%ms%

:: mission accomplished
set /a totalsecs = %hours%*3600 + %mins%*60 + %secs% 
echo command took %hours%:%mins%:%secs%.%ms% (%totalsecs%.%ms%s total)

Usage

If you put timecmd.bat in a directory in your path, you can call it from anywhere like this:

timecmd [your command]

e.g.

C:\>timecmd pause
Press any key to continue . . .
command took 0:0:1.18

If you want to do output redirection, you can quote the command like this:

timecmd "dir c:\windows /s > nul"

This should handle commands that run from before- to after-midnight, but the output will be wrong if your command runs for 24 hours or more.

share|improve this answer
3  
Replace :: run your command here with %* so you can use this script like so: timethis.bat dir C:\windows /s > nul or timethis.bat cmd /c "pause&pause". – Scott Stafford Aug 7 '12 at 0:38
Thanks @ScottStafford, I've incorporated your suggestion :) – Luke Sampson Aug 8 '12 at 2:28
2  
np... took the liberty of adding a couple smaller tweaks (@setlocal and totalseconds) – Scott Stafford Aug 8 '12 at 14:58
awesome, thanks again :) – Luke Sampson Aug 9 '12 at 22:34
2  
replacing cmd /c %* with %* works unless your command is another batch file. Then you'd have to do timecmd call other.bat – Jesse Chisholm Mar 27 at 16:50
show 1 more comment

If you have a command window open and call the commands manually, you can display a timestamp on each prompt, e.g.

prompt $d $t $_$P$G

gives you something like

23.03.2009 15:45:50,77

C:\>

if you have a small batch script that executes your commands, have an empty line before each command, e.g.

(empty line)

myCommand.exe

(next empty line)

myCommand2.exe

you can calculate the execution time for each command by the time info in the prompt. The best would probably be to pipe the output to a textfile for further analysis:

MyBatchFile.bat >output.txt
share|improve this answer
Thought I'd drop in a couple years behind the discussion to thank you for sharing the prompt command. I've seen a lot of CMD tricks (more of a bash guy), but this one escaped my notice. – Steve Howard Dec 5 '11 at 14:40

The one-liner I use in Win2008 R2 is:

cmd /v:on /c "echo !TIME! & *mycommand* & echo !TIME!"

So long as mycommand doesn't require quotes (which screws with cmd's quote processing). The /v:on is to allow for the two different TIME values to be evaluated independently rather than once at the execution of the command.

share|improve this answer
Perfect, exactly what I was looking for! – Frerich Raabe May 26 '11 at 6:56

Since others are recommending installing things like freeware and PowerShell, you could also install Cygwin, which would give you access to many basic Unix commands like time:

abe@abe-PC:~$ time sleep 5

real    0m5.012s
user    0m0.000s
sys 0m0.000s

Not sure how much overhead Cygwin adds.

share|improve this answer
5  
When answering Windows questions, Cygwin is considered cheating :-) – mivk Dec 13 '11 at 22:08

Not quite as elegant as some of the functionality in unix, but create a cmd file which looks like:

@echo off
time < nul
yourexecutable.exe > c:\temp\output.txt
time < nul
rem on newer windows system you can try time /T

That will display the start and stop times like so:

The current time is: 10:31:57.92
Enter the new time: 
The current time is: 10:32:05.94
Enter the new time:
share|improve this answer
2  
Assuming you don't need portability across different languages you can also put a findstr "current" or something like that after the "time < nul" (nice usage of nul by the way; I've always used echo.|time, but that doesn't look as elegant :)). And use "for" to extract only the time, not the text. – Јοеу Mar 23 '09 at 18:24
If command extensions are enabled (that is, the default), the Win XP version of time has a /t option which just displays the current system time, without prompting you to enter a new time. However when you do that it only displays hours and minutes, which might not be good enough for some usages. (See John Snow's answer to this question.) – martineau Jan 23 '11 at 18:34
1  
time /T might also work. – Elazar Leibovich Sep 28 '11 at 6:26
time /T is usable only if the process runs for several minutes! time < nul is uglier but more precise. – PhiLho Jun 4 at 10:59

I use a freeware called "GS Timer".

Just make a batch file like this:

timer
yourapp.exe
timer /s

If you need a set of times, just pipe the output of timer /s into a .txt file.

You can get it here: GS Timer at SoftPedia

share|improve this answer
I don't think using a third party application could be considered doing it "with standard means". – martineau Jan 23 '11 at 18:37
1  
The questioner meant "without installing additional software", but then the top (accepted) answer also requires installing a whole Windows 2003 Resource Kit! Anyway, timer.exe was perfect for what I was just looking for, thanks! – Hugo May 27 '11 at 10:32
This is a planted link. SoftPedia is a click farm. – Tom A Sep 4 '12 at 23:47
@Tom please explain, what do you mean "planted link"? – pepoluan Sep 6 '12 at 8:26
@pepoluan: Every link on that page just opens another window with more ads. None of them start an actual download. It was a very frustrating waste of time for me. I think one of the second-page links even tried to install a download manager exe -- like I would trust software from a site like that! – Tom A Sep 7 '12 at 20:27
show 8 more comments

As long as it doesn't last longer than 24hours...

@echo off

set starttime=%TIME%
set startcsec=%STARTTIME:~9,2%
set startsecs=%STARTTIME:~6,2%
set startmins=%STARTTIME:~3,2%
set starthour=%STARTTIME:~0,2%
set /a starttime=(%starthour%*60*60*100)+(%startmins%*60*100)+(%startsecs%*100)+(%startcsec%)

:TimeThis
ping localhost 

set endtime=%time%
set endcsec=%endTIME:~9,2%
set endsecs=%endTIME:~6,2%
set endmins=%endTIME:~3,2%
set endhour=%endTIME:~0,2%
if %endhour% LSS %starthour% set /a endhour+=24
set /a endtime=(%endhour%*60*60*100)+(%endmins%*60*100)+(%endsecs%*100)+(%endcsec%)

set /a timetaken= ( %endtime% - %starttime% )
set /a timetakens= %timetaken% / 100
set timetaken=%timetakens%.%timetaken:~-2%

echo.
echo Took: %timetaken% sec.
share|improve this answer
Impressive, but I don't think it works if there's an 08 or 09 in the start or end times because these are interpreted as octals. My answer handles that :) – Luke Sampson Jun 2 '11 at 1:26

I'm using Win XP and for some reason timeit.exe does not work for me. I found another alternative - PTIME. This works very good.

http://www.pc-tools.net/win32/ptime/

share|improve this answer

There's also TimeMem (March 2012):

This is a Windows utility which executes a program and displays its execution time, memory usage, and IO statistics. It is similar in functionality to the Unix time utility.

share|improve this answer
  1. In the directory where your program is, type notepad mytimer.bat, click yes to create a new file.

  2. Paste the code below, replacing YourApp.exe with your program, then save.

@echo off

date /t

time /t

YourApp.exe

date /t

time /t

  1. Type mytimer.bat in the command line then press Enter.
share|improve this answer
4  
time /t only gives you time in HH:MM. To time an executable, you usually needs more accuracy... Is it anything you can setup to get down to fractions of a second? I tested the sollution from JohnW (time < nul), and that actually gives the time down to 1/100s: HH:MM:SS.XX – awe Aug 17 '09 at 6:03

In case anyone else has come here looking for an answer to this question, there's a Windows API function called GetProcessTimes(). It doesn't look like too much work to write a little C program that would start the command, make this call, and return the process times.

share|improve this answer
It doesn't look like too much work to download a little C program that would start the command, make this call (and much more advance measurements), and return the process times. – Elazar Leibovich Sep 28 '11 at 7:24

Process Explorer will show kernel time, user time, and wall time (and lots of other stuff) as long as you click on the process before it exits. Not a command-line tool, but immensely useful anyway.

share|improve this answer
@echo off & setlocal

set start=%time%

REM Do stuff to be timed here.
REM Alternatively, uncomment the line below to be able to
REM pass in the command to be timed when running this script.
REM cmd /c %*

set end=%time%

REM Calculate time taken in seconds, to the hundredth of a second.
REM Assumes start time and end time will be on the same day.

set options="tokens=1-4 delims=:."

for /f %options% %%a in ("%start%") do (
    set /a start_s="(100%%a %% 100)*3600 + (100%%b %% 100)*60 + (100%%c %% 100)"
    set /a start_hs=100%%d %% 100
)

for /f %options% %%a in ("%end%") do (
    set /a end_s="(100%%a %% 100)*3600 + (100%%b %% 100)*60 + (100%%c %% 100)"
    set /a end_hs=100%%d %% 100
)

set /a s=%end_s%-%start_s%
set /a hs=%end_hs%-%start_hs%

if %hs% lss 0 (
    set /a s=%s%-1
    set /a hs=100%hs%
)
if 1%hs% lss 100 set hs=0%hs%

echo.
echo  Time taken: %s%.%hs% secs
echo.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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