1

I wrote following program just to test the accuracy of Windows Batch file commands TIMEOUT, PAUSE and SLEEP:

@echo off
setlocal

rem The format of %TIME% is HH:MM:SS,CS for example 23:59:59,99
set STARTTIME=%TIME%
TIMEOUT 10
set ENDTIME=%TIME%

rem output as time
echo STARTTIME: %STARTTIME%
echo ENDTIME: %ENDTIME%

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

rem calculating the duratyion is easy
set /A DURATION=%ENDTIME%-%STARTTIME%

rem we might have measured the time in between days
if %ENDTIME% LSS %STARTTIME% set set /A DURATION=%STARTTIME%-%ENDTIME%

rem now break the centiseconds down to hors, minutes, seconds and the remaining centiseconds
set /A DURATIONH=%DURATION% / 360000
set /A DURATIONM=(%DURATION% - %DURATIONH%*360000) / 6000
set /A DURATIONS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000) / 100
set /A DURATIONHS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000 - %DURATIONS%*100)

rem some formatting
if %DURATIONH% LSS 10 set DURATIONH=0%DURATIONH%
if %DURATIONM% LSS 10 set DURATIONM=0%DURATIONM%
if %DURATIONS% LSS 10 set DURATIONS=0%DURATIONS%
if %DURATIONHS% LSS 10 set DURATIONHS=0%DURATIONHS%

rem outputing
echo STARTTIME: %STARTTIME% centiseconds
echo ENDTIME: %ENDTIME% centiseconds
echo DURATION: %DURATION% in centiseconds
echo %DURATIONH%:%DURATIONM%:%DURATIONS%,%DURATIONHS%

endlocal
goto :EOF

I got following outputs in multiple runs: STARTTIME: 17:49:47.47 ENDTIME: 17:49:57.17 STARTTIME: 6418747 centiseconds ENDTIME: 6419717 centiseconds DURATION: 970 in centiseconds 00:00:09,70

STARTTIME: 17:52:00.84
ENDTIME: 17:52:10.15
STARTTIME: 6432084 centiseconds
ENDTIME: 6433015 centiseconds
DURATION: 931 in centiseconds
00:00:09,31

STARTTIME: 17:53:02.36
ENDTIME: 17:53:12.14
STARTTIME: 6438236 centiseconds
ENDTIME: 6439214 centiseconds
DURATION: 978 in centiseconds
00:00:09,78

STARTTIME: 17:55:22.14
ENDTIME: 17:55:32.16
STARTTIME: 6452214 centiseconds
ENDTIME: 6453216 centiseconds
DURATION: 1002 in centiseconds
00:00:10,02

STARTTIME: 17:55:38.49
ENDTIME: 17:55:48.11
STARTTIME: 6453849 centiseconds
ENDTIME: 6454811 centiseconds
DURATION: 962 in centiseconds
00:00:09,62

Can any one explain output varies so very much and why is duration not close to 10? The error is around 4%.

  • `timeout` does actually not wait for the given amount of time, but awaits ticks of second multiples, that is, the first tick could occur immediately or last one second at most, so for instance `timeout /T 3` waits for a time > 2s and <= 3s... to wait for exactly 3s, use `ping 127.0.0.1 -n 4` (4 attempts with intervals of 1s each)... – aschipfl Dec 27 '15 at 12:47
  • thanks for the reply..... – Shikhar Bhargava Dec 27 '15 at 12:53
  • `if %DURATION% LSS 0 set /A "DURATION+=8640000"` (add 24 hours in centiseconds) to measure the time in between days... – JosefZ Dec 28 '15 at 22:41

1 Answers1

3

timeout does actually not wait for the given amount of time, but awaits second ticks (or second multiples), that is, the first tick could occur immediately when the command is run, or it could last one second at most, depending on the point of time the command is started.

So, for instance, timeout /T 3 waits for a time greater than 2 seconds and 3 seconds at most, because the first tick occurs > 0s and <= 1s after the command runs, and the remaining 2 ticks occur after another full second each.

To wait for exactly 3s, use ping 127.0.0.1 -n 4 (4 ping attempts with 3 intervals of 1s each).

Note:
To measure the execution time of a command more precisely, you might be interested in this method.

Community
  • 1
  • 1
aschipfl
  • 28,946
  • 10
  • 45
  • 77