I'm using a batch script for the time measurement of commands. I have found this script here on StackOverflow. Unfortunately it sometimes is not working properly and unfortunately I don't know much about batch programming. I get the impression that it sometimes works and sometimes not. Perhaps I err on this but it does seem that its not working properly after midnight.
The script:
@echo off
setlocal
rem The format of %TIME% is HH:MM:SS,CS for example 23:59:59,99
set STARTTIME=%TIME%
rem here begins the command you want to measure
dir /s > nul
rem here ends the command you want to measure
set ENDTIME=%TIME%
echo STARTTIME %STARTTIME%
rem convert STARTTIME and ENDTIME to centiseconds
set /A STARTTIME=(1%STARTTIME:~0,2%-100)*360000 + (1%STARTTIME:~3,2%-100)*6000 + (1%STARTTIME:~6,2%-100)*100 + (1%STARTTIME:~9,2%-100)
echo STARTTIME %STARTTIME%
echo ENDTIME %ENDTIME%
set /A ENDTIME=(1%ENDTIME:~0,2%-100)*360000 + (1%ENDTIME:~3,2%-100)*6000 + (1%ENDTIME:~6,2%-100)*100 + (1%ENDTIME:~9,2%-100)
echo ENDTIME %ENDTIME%
rem calculating the duration is easy
set /A DURATION=%ENDTIME%-%STARTTIME%
rem we might have measured the time inbetween days
if %ENDTIME% LSS %STARTTIME% set set /A DURATION=%STARTTIME%-%ENDTIME%
rem outputing
echo DURATION: %DURATION% in centiseconds
endlocal
goto :EOF
I was using it half an hour ago and I got this result:
STARTTIME 1:29:25.17
STARTTIME 1:29:25.17
ENDTIME 1:29:27.82
ENDTIME 1:29:27.82
DURATION: 1 in centiseconds
So something with the lines
set /A STARTTIME=(1%STARTTIME:~0,2%-100)*360000 + (1%STARTTIME:~3,2%-100)*6000 + (1%STARTTIME:~6,2%-100)*100 + (1%STARTTIME:~9,2%-100)
and
the line
set /A ENDTIME=(1%ENDTIME:~0,2%-100)*360000 + (1%ENDTIME:~3,2%-100)*6000 + (1%ENDTIME:~6,2%-100)*100 + (1%ENDTIME:~9,2%-100)
seems to be wrong.
What is it?
Regards, David
The problem is with "%STARTTIME:~0,2%-100"
being " 1"
, there is an extra space. The program then tries to find the value of (1 1)
instead of (101)
.
The extra 1 in front should only be for systems that output the time as "01:29:25.17"
(to give 101 instead of 01 which the system will interpret as octal number), but for systems that output the time as " 1:29:25.17"
(Note the space in front of the 1) there is a problem (which will give 1 1
).
An easy way is just to remove the 1 in STARTTIME=(1%STARTTIME:~0,2%-100)*360000
and ENDTIME=(1%ENDTIME:~0,2%-100)*360000
, though that might cause a problem with other systems.
A more watertight solution would be to add two lines after the rem line:
rem convert STARTTIME and ENDTIME to centiseconds
set STARTTIME=%STARTTIME: =0%
set ENDTIME=%ENDTIME: =0%
(EDIT: Andriy M's much shorter method above)
There are also two set
in if %ENDTIME% LSS %STARTTIME% set set /A DURATION=%STARTTIME%-%ENDTIME%
which might cause a problem when trying to measure the time between days.