Timing for commands

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Timing for commands

#1 Post by miskox » 07 Apr 2016 11:46

Hi all!

I would like to measure which command is faster:

Code: Select all

@echo off
echo TEST>test.tmp
echo %time%
del temp.tmp>nul 2>nul
echo TEST>test.tmp
echo %time%
if exist temp.tmp del temp.tmp
echo %time%


Result:

Code: Select all

c:\test.cmd
19:42:03,46
19:42:03,46
19:42:03,46
c:\


I would say

Code: Select all

del temp.tmp>nul 2>nul


is faster but the other

Code: Select all

if exist temp.tmp del temp.tmp


is more 'by the book' how it should be programmed because there are no errors displayed/triggered.

How to measure more precisely?

Thanks.
Saso

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Timming for commands

#2 Post by foxidrive » 07 Apr 2016 20:17

I just noted that you are creating test.tmp
but you are deleting temp.tmp

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Timming for commands

#3 Post by miskox » 08 Apr 2016 01:46

foxidrive wrote:I just noted that you are creating test.tmp
but you are deleting temp.tmp


Good eyes! Thanks.

Anyway I get same results (within a hundredth of a second).

Here is the correct code:

Code: Select all

@echo off
echo TEST>test.tmp
echo %time%
del test.tmp>nul 2>nul
echo TEST>test.tmp
echo %time%
if exist test.tmp del test.tmp
echo %time%


Saso

einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Timming for commands

#4 Post by einstein1969 » 08 Apr 2016 11:38

For timing a piece of code more precisely you must use a cicle FOR.

set ITER=1000
For /L %%L in (1,1,%ITER%) do (
%= insert code to timing here =%
)

get the time then divide by %ITER%

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Timming for commands

#5 Post by miskox » 11 Apr 2016 12:27

I will use this with a higher number of course.

Thanks.
Saso

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Timming for commands

#6 Post by Squashman » 11 Apr 2016 13:36

You may to consider using Dave's getTimestamp.bat
viewtopic.php?t=4847

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Timming for commands

#7 Post by pieh-ejdsch » 28 Apr 2016 07:45

this some tests with timings:
goes up to 200 frames and more eg 1000 calculates itself

Code: Select all

@echo off
setlocal enabledelayedexpansion
prompt $g$s
:setCheckframes
set /a  Frames    = %1  &:: soll je sekunde
if !frames! lss 1 set /a Frames = 5
set frames
set /a  Stops     =20       &::    10251 mit 2 Runden = 50 Hertz
set /a Rounds    =frames *2
if !rounds! gtr 50 set /a rounds =10
set /a kFrames    =0 ,x =0 ,loops =0 ,n=0
set "permille= x "
@rem :: @       &rem :: to many
@rem :: set /a /1 2>nul  &rem :: 770 5-fps (1x  22% ) &rem :: 390 5-fps (2x 22%)  (CPU)
@rem :: "Kein Befehl" 2>nul &rem :: 145 5-fps (1x  23% )
:Checkframes
:: reale milliseconds
set /a "StrTime = 1000 *(60*(60*(!time:~-11,2! %%24) +60!time:~-8,2! %%60) +60!time:~-5,2! %%60) +100!time:~-2!0 %%1000"
set /a "t = !time:~-4,1!"
:echo %strtime%
for /L %%i in (1 1 %rounds%) do (for /L %%N in (0 1 %stops%) do rem comment
) & (
 set /a x+=1 &<nul set /p =.&if !t! neq !time:~^-4^,1! title !time! frames: !x! per second  !permille!/1000  !stops! .&set /a "t=!time:~-4,1! ,x=0"
)
set /a "EndTime = 1000 *(60*(60*(!time:~-11,2! %%24) +60!time:~-8,2! %%60) +60!time:~-5,2! %%60) +100!time:~-2!1 %%1000"
:echo on
set /a ms = (EndTime +86400000) %% 86400000 - StrTime
set /a stops = stops *1000 *rounds /ms /frames ,permille = frames *ms /rounds

rem some tests
:: rem smooth ovr 0,3 sek ; max x %
:if defined chkone if ms lss 300 set /a rounds = frames *ms /permille
:: rem small unr 2 sek

::if !stops! lss 1 set /a rounds  = rounds *1000 /permille, stops =stops + stops /rounds +1
:if !stops! gtr !rounds!  if !permille! gtr 1100 set /a rounds  = rounds *1000 /permille, stops =stops - stops /rounds +1

:nostops
if !perMille! neq 1000 goto :Checkframes
set /a loops +=1
if !loops! lss 10 goto :Checkframes
echo(
pause
exit /b


and for other tests

Code: Select all

@echo off & setlocal EnableDelayedExpansion
rem Reference test

rem 35.3365
echo ERROR x ca 3.500
set t0=%time%
For /L %%i in (1,1,3534) do set /a /1 2>nul
call :difftime "%t0%" "%time%" t1

rem ::::::::::::::::::::::::::::::::::::::::
rem 813.0081
Echo Komment x ca 81.300
set t0=%time%
For /L %%i in (1,1,81301) do rem this is a comment
call :difftime "%t0%" "%time%" t2

rem ::::::::::::::::::::::::::::::::::::::::

rem 1267.4271
Echo rem x ca 126.700
set t0=%time%
For /L %%i in (1,1,126742) do rem
call :difftime "%t0%" "%time%" t3

rem ::::::::::::::::::::::::::::::::::::::::

rem 4807.6923
 echo @ x ca 480.800
set t0=%time%
For /L %%i in (1,1,480769) do @

call :difftime "%t0%" "%time%" t4

rem ::::::::::::::::::::::::::::::::::::::::

rem 5.8139
echo KEIN Befehl x ca 580
set t0=%time%
For /L %%i in (1,1,581) do "kein befehl" 2>nul
call :difftime "%t0%" "%time%" t5

rem :::::::::::::::::::::::::::::::::::::::
echo ping  -n 1 x 22
set t0=%time%
for /L %%i in (1 1 22) do ping -n 1 localhost >nul
call :difftime "%t0%" "%time%" t6

rem :::::::::::::::::::::::::::::::::::::::
echo ping -n 2
set t0=%time%
for /L %%i in (1 1 1) do ping -n 2 localhost >nul
call :difftime "%t0%" "%time%" t7

rem calculate percent

set /a p1=100-t1*100/t2

echo %t1% is %p1%%% more speed than %t2%

set /a p1=100-t1*100/t3

echo %t1% is %p1%%% more speed than %t3%

set /a p1=100-t1*100/t4

echo %t1% is %p1%%% more speed than %t4%

set /a p1=100-t1*100/t5

echo %t1% is %p1%%% more speed than %t5%

set /a p1=100-t1*100/t6

echo %t1% is %p1%%% more speed than %t6%

set /a p1=100-t1*100/t7

echo %t1% is %p1%%% more speed than %t7%

exit /b

:difftime
  setlocal
   set t0=%~1
   set t1=%~2
   for /F "tokens=1-8 delims=:.," %%a in ("%t0: =0%:%t1: =0%") do set /a "a=(((1%%e-1%%a)*60)+1%%f-1%%b)*6000+1%%g%%h-1%%c%%d, a+=(a>>31) & 8640000"
   echo %t0% - %t1% elapsed %a% cs
  endlocal & set %3=%a%
exit /b

Post Reply