Timer in Windows batch file Timer in Windows batch file windows windows

Timer in Windows batch file


Refactored the code to calculate the elapsed time:

  • less code in calculation,
  • provided padding for elapsed time less than .1 seconds,
  • using labels to allow calling it multiple times, and to group the timer code.

Reusable code:

:StartTimer:: Store start timeset StartTIME=%TIME%for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo %StartTIME: =0%`) do set /a Start100S=1%%f*360000+1%%g*6000+1%%h*100+1%%i-36610100goto :EOF:StopTimer:: Get the end timeset StopTIME=%TIME%for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo %StopTIME: =0%`) do set /a Stop100S=1%%f*360000+1%%g*6000+1%%h*100+1%%i-36610100:: Test midnight rollover. If so, add 1 day=8640000 1/100ths secsif %Stop100S% LSS %Start100S% set /a Stop100S+=8640000set /a TookTime=%Stop100S%-%Start100S%set TookTimePadded=0%TookTime%goto :EOF:DisplayTimerResult:: Show timer start/stop/deltaecho Started: %StartTime%echo Stopped: %StopTime%echo Elapsed: %TookTime:~0,-2%.%TookTimePadded:~-2% secondsgoto :EOF

Calling code:

call :StartTimer:: :: Add your script functionality here::call :StopTimercall :DisplayTimerResultpausecall :StartTimer:: :: Add more script functionality here::call :StopTimercall :DisplayTimerResultgoto :EOF

Note the "1"-prefix to avoid interpretation errors of zero-padded values as octal values (08 and 09), and the correction with the appropriate constant. As this might be confusing to understand the core calculation, an alternative to the one-line for statement would be something like the following:

for /f "usebackq tokens=1-4 delims=:., " %%f in (`echo.%StartTIME%`) do set TempTIME=%%f %%g %%h %%ifor /f "usebackq tokens=1-4" %%f in (`echo %TempTIME: 0= %`) do set /a Start100S=%%f*360000+%%g*6000+%%h*100+%%i


A minimalistic version with elapsed time in seconds.

@set /A _tic=%time:~0,2%*3600^            +%time:~3,1%*10*60^            +%time:~4,1%*60^            +%time:~6,1%*10^            +%time:~7,1% >nul:: actual script@set /A _toc=%time:~0,2%*3600^            +%time:~3,1%*10*60^            +%time:~4,1%*60^            +%time:~6,1%*10^            +%time:~7,1% >nul@set /A _elapsed=%_toc%-%_tic@echo %_elapsed% seconds.


As far as I know there is no explicit 'timer' command you can use in batch files - however there is a fairly simple solution. At the top of your batch file record the current time (start time) at the end of the batch file record the current time (end time) and then compare the two. Something like this should do it (it does seem overly complicated but it handles most quirks of time):

:: Store start timeset StartTIME=%TIME%set H=%StartTIME:~0,2%if "%H:~0,1%"==" " set H=%H:~1,1%if "%H:~0,1%"=="0" set H=%H:~1,1%set M=%StartTIME:~3,2%if "%M:~0,1%"=="0" set M=%M:~1,1%set S=%StartTIME:~6,2%if "%S:~0,1%"=="0" set S=%S:~1,1%set U=%StartTIME:~9,2%if "%U:~0,1%"=="0" set U=%U:~1,1%)set /a Start100S=%H%*360000+%M%*6000+%S%*100+%U%:: :: Add your script functionality here:::: Get the end timeset StopTIME=%TIME%set H=%StopTIME:~0,2%if "%H:~0,1%"==" " set H=%H:~1,1%if "%H:~0,1%"=="0" set H=%H:~1,1%set M=%StopTIME:~3,2%if "%M:~0,1%"=="0" set M=%M:~1,1%set S=%StopTIME:~6,2%if "%S:~0,1%"=="0" set S=%S:~1,1%set U=%StopTIME:~9,2%if "%U:~0,1%"=="0" set U=%U:~1,1%)set /a Stop100S=%H%*360000+%M%*6000+%S%*100+%U%:: Test midnight rollover. If so, add 1 day=8640000 1/100ths secsif %Stop100S% LSS %Start100S% set /a Stop100S+=8640000set /a TookTime=%Stop100S%-%Start100S%echo Started: %StartTime%echo Stopped: %StopTime%echo Elapsed: %TookTime:~0,-2%.%TookTime:~-2% seconds

Alternatively #1 : The Windows Server 2k3 resource kit includes timeit.exe. You can use this to display various performance stats for your script.

Alternatively #2 : You could go much simpler than the script posted above and do this:

echo %time% :::: Your script functionality::echo %time%

However, this won't give you execution time in seconds.