10

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.

4
  • Might belong on Stackoverflow.
    – Joey
    Jul 30, 2009 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, 2009 at 16:42
  • More likely on Serverfault. Jul 30, 2009 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, 2009 at 16:54

7 Answers 7

22

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.

3
  • 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, 2014 at 9:49
  • That sucks. I still have the executable lying around here from old times, though.
    – Joey
    Jan 4, 2014 at 10:53
  • @pesche: I added a batch file that can act as a drop-in replacement for timethis.
    – Joey
    Jan 4, 2014 at 11:25
4

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
--------------
0
4

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
2
  • Unfortunately, "time /t" does not output seconds, just "hour:minute am/pm".
    – slolife
    Aug 3, 2009 at 18:28
  • right you are, i have updated the answer.
    – akf
    Aug 3, 2009 at 19:22
1

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())";
1
In command prompt:
 wmic path win32_operatingsystem get LocalDateTime
 wmic path win32_operatingsystem get LocalDateTime
 wmic path win32_operatingsystem get LocalDateTime

Output:
C:\Users\Steini> wmic path win32_operatingsystem get LocalDateTime
LocalDateTime
20180113135409.370000+000

C:\Users\Steini>wmic path win32_operatingsystem get LocalDateTime
LocalDateTime
20180113135409.525000+000

C:\Users\Steini>wmic path win32_operatingsystem get LocalDateTime
LocalDateTime
20180113135409.627000+000

15,5ms and 10,2ms using Ctrl + v from clipboard

0

Far Manager 1.x users can consider Timer plugin.

0

Here is a way to time a command (accurate, milliseconds also):

@echo off & setlocal EnableDelayedExpansion
set "start=!time!"
::***********************************************************
:: YOUR COMMAND GOES HERE
::***********************************************************
set "end=!time!"
for /f "tokens=1-4 delims=:.," %%a in ("%start%") do set /a "_start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
for /f "tokens=1-4 delims=:.," %%a in ("%end%") do set /a "_end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
set /a elapsed=_end-_start
set /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
for %%a in (hh mm ss cc) do if "!%%~a!" LSS 10 set "%%~a=0!%%~a!"
set "duration=%hh%:%mm%:%ss%.%cc%"
echo Start Time: %start%
echo End Time: %end%
echo ----------------
echo Duration: %duration%
pause
exit /b 0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .