Easiest way to time a command line tool

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.


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

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