Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What is the easiest/quickest way to time a command line tool?

It doesn't have to be super accurate (+/- seconds is okay) since I expect this to take minutes or even 1 hour+. And writing a batch file is fine too. Also, it doesn't need to do calculations on time, just display the start time and end time.

share|improve this question
    
Might belong on Stackoverflow. –  Joey Jul 30 '09 at 16:40
2  
If I have to "program" something to get this to work... the solution is too complicated. There must be some command line tool or simple batch solution for this, no? –  slolife Jul 30 '09 at 16:42
    
More likely on Serverfault. –  Kirill V. Lyadvinsky Jul 30 '09 at 16:43
    
Generally the OS only cares about how much CPU time it has used. My answer below will do what you want. I just use a log file to store the information. –  Axxmasterr Jul 30 '09 at 16:54

4 Answers 4

up vote 18 down vote accepted

I am usually using

echo.|time & my_command & echo.|time

when I have nothing else at hand. This causes output like the following:

> echo.|time & ping -n 4 localhost > nul & echo.|time
The current time is: 18:42:34,63
Enter the new time:
The current time is: 18:42:37,68
Enter the new time:

Not pretty and can be made prettier by piping to findstr:

echo.|time|findstr current & ping -n 4 localhost > nul & echo.|time|findstr current

If you have delayed expansion enabled by default (or started cmd with /v:on as argument) you can also just use echo !time! without having to resort to ugly hacks with input redirection.

If you want to use a batch file, you can do it like this:

@echo Start time: %time%
@%*>nul 2>nul
@echo End time: %time%

I have added redirection to nul for both stdout and stderr here, because otherwise it might be difficult to find the start and end lines. You may remove this if this is of no concern to you.

But nowadays I mostly use TimeThis – which by now was removed, sadly.

PowerShell offers a way as well:

Measure-Command { my_command }

but you need to be careful with things that rely on the working directory or redirection. For those to work correctly you might need a little trickery:

@echo off
setlocal enableextensions

rem Prepare current directory and command line so they
rem can be stuck into a single-quoted PowerShell string.
set "Dir=%CD:'=''%"
set "Cmd=%*"
set "Cmd=%Cmd:'=''%"

rem Prepare command to pass to PowerShell
set Command=
set "Command=&{"
set "Command=%Command%  Set-Location '%Dir%'"
set "Command=%Command%; [Environment]::CurrentDirectory = '%Dir%'"
set "Command=%Command%; $start = Get-Date"
set "Command=%Command%; Write-Host '    Command line : %Cmd%'"
set "Command=%Command%; Write-Host ('    Start time   : ' + $start.ToString())"
set "Command=%Command%; Write-Host"
set "Command=%Command%; iex 'cmd /c %Cmd%'"
set "Command=%Command%; $end = Get-Date"
set "Command=%Command%; Write-Host"
set "Command=%Command%; Write-Host '    Command line : %Cmd%'"
set "Command=%Command%; Write-Host ('    Start time   : ' + $start.ToString())"
set "Command=%Command%; Write-Host ('    End time     : ' + $end.ToString())"
set "Command=%Command%; Write-Host ('    Elapsed time : ' + ($end - $start).ToString())"
set "Command=%Command%; }"

powershell -noprofile -command "%Command%"

endlocal

This can be run the same way as timethis, be sure to escape double quotes with \" if they are needed in the command line (same as timethis as well). The output produced is similar. Redirections won't work, though.

share|improve this answer
    
Thanks for the excellent info! –  slolife Jul 30 '09 at 16:51
    
TimeThis is from Windows 2000 resource kit and no longer available; in 2003 Resource Kit is TimeIt, which didn't work on my 64bit Win7. For a PowerShell solution see superuser.com/questions/228056/… –  pesche Jan 4 at 9:49
    
That sucks. I still have the executable lying around here from old times, though. –  Joey Jan 4 at 10:53
    
@pesche: I added a batch file that can act as a drop-in replacement for timethis. –  Joey Jan 4 at 11:25

You can use the system scheduler to run the command for you.

Control Panel -> Scheduled Tasks -> create new....

Just point the scheduled task at the command you want to run and it should do what you want.

If you want to create a logfile that stores time state information you might want to consider doing something similar to the script above.

I use a utility called "now" for doing this as it will echo the date and time. This is very handy for command line scripts that you want to keep track of in log files but lack the smarts to include a date/time natively. Very handy as it does not care about a response and it goes away when you are done with it.

Now Utlity can be found Here

Script example would be...

echo -------------- >>logfile.txt
now Process Starting>> logfile.txt
<commandhere> <target> >> logfile.txt
now process Ending >> logfile.txt
echo -------------- >> logfile.txt

Sample output would look like

--------------
Mon Mar 06 14:58:48 2009 Process Starting
GNU Super Duper Process PID 12345! Success
Mon Mar 06 21:47:01 2009 Process Ending
--------------
share|improve this answer
    
+1 for the Now utility link –  slolife Jul 30 '09 at 17:17

another option is to use the time command with /t which will put the current time out to the console (or redirect to a log) without prompting for the time to be set. there is a limitation in that it will give you the hours, minutes, but not seconds and milliseconds.

call time /t  > myLog.log
call mybat   >> myLog.log
call time /t >> myLog.log
share|improve this answer
    
Unfortunately, "time /t" does not output seconds, just "hour:minute am/pm". –  slolife Aug 3 '09 at 18:28
    
right you are, i have updated the answer. –  akf Aug 3 '09 at 19:22

If you have powershell, you can do this (all one line, broken up for readability):

PS C:\> $startTime=Get-Date;ping /n 4 localhost;$endTime=Get-Date;
        echo "Finished in $(($endTime-$startTime).ToString())"

Or save this to a file for easier reuse.

# usage: .\timer.ps1 program [arguments]

$startTime = Get-Date;
$prog = $args[0];
$progargs = $args[1..($args.length-1)];

&$prog $progargs

$endTime = Get-Date;

echo "Finished in $(($endTime - $startTime).ToString())";
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.