Home » Questions » Computers [ Ask a new question ]

Easiest way to time a command line tool

Easiest way to time a command line tool

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

Asked by: Guest | Views: 24
Total answers/comments: 4
Guest [Entry]

"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."
Guest [Entry]

"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"
Guest [Entry]

Far Manager 1.x users can consider Timer plugin.
Guest [Entry]

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