Mostly Linux & Python syntax notes and hyperlinks.

Friday, April 21, 2017

Mediocre Windows Batch Script to Measure Time

Well, it uses clock time, and would be more useful as a called subroutine, but this is what I've got so far:

rem Test Timing

rem You might need to set this in later versions and also use !time 

setlocal enabledelayedexpansion

set start_time=%time%
echo start_time is %start_time%

pause
rem Replace above 'pause' with activity to be timed (approx).

set end_time=%time%

rem Now calculate time difference


echo started %start_time%
echo ended %end_time%


rem To get a substring, do this:
rem    %variable:~num_chars_to_skip,num_chars_to_keep%

rem Here we also prepend "1" to 2-digits and then subtract 100 
rem   to avoid numbers like "02"
rem   which can cause errors in the windows batch interpreter.

set sh=1%start_time:~0,2%
set eh=1%end_time:~0,2%
set sm=1%start_time:~3,2%
set em=1%end_time:~3,2%
set ss=1%start_time:~6,2%
set es=1%end_time:~6,2%
set sms=1%start_time:~9,2%
set ems=1%end_time:~9,2%

echo sh %sh%, eh %eh%, sm %sm%, em %em% and sms %sms%, ems %ems%
pause

set /A start_secs=(%sh%-100)*3600 + (%sm%-100)*60 + (%ss%-100)
set /A end_secs=(%eh%-100)*3600 + (%em%-100)*60 + (%es% - 100)

set /A start_ms=(%start_secs%)*100 + (%sms% - 100)
set /A end_ms=(%end_secs%)*100 + (%ems% - 100)

set /A diff=%end_ms% - %start_ms%

echo %start_ms% to %end_ms% is %diff% milliseconds 

pause

Lots of "pause"s to check how it's going.
You can take them out.
Better versions welcome, too.