1.

Solve : Timing a command.?

Answer»

Sorry, I know for a fact this has been posted before but I cannot seem to find it.
I want to see how long a command takes to execute, but the problem at the moment is that when I minus the start time from the end time it cannot do it because it contains :.
Here is what I have so far:
Code: [Select]set start=%time%
command
set finish=%time%
set /a length=%finish%-%start%
echo %length%
if your %time% looks like
Code: [Select]20:33:46,92
then

Code: [Select]set start=%time%
command
set finish=%time%

set start=%start::=%
set start=%start:,=%
set finish=%finish::=%
set finish=%finish:,=%

set /a length=%finish%-%start%
echo %length%My time Is 00:00:00.00
I adjusted your code, and managed to get something remotely correct, thank you.

Code: [Select]set start=%time%
command
set finish=%time%

set start=%start::=%
set start=%start:.=%
set finish=%finish::=%
set finish=%finish:.=%

set /a length=%finish%-%start%
set /a length=%length%/100
echo %length% SECONDS
it may "work", but it won't be accurate, because there are more than 10 seconds in a minute, and more than 10 minutes in an hour. (in other words, the "number" PRODUCED is not a true decimal number you can do MATH with.)

Furthermore, it may well crash if it has to process a time before 10 AM.
elapsed-calc.bat

Code: [Select]@echo off
REM remove surrounding QUOTES (if any)
set time1=%~1
set time2=%~2
set hh1=%time1:~0,2%
set mm1=%time1:~3,2%
set ss1=%time1:~6,2%
set cs1=%time1:~9,2%
if "%hh1:~0,1%"==" " set hh1=%hh1:~1,1%
if "%mm1:~0,1%"=="0" set mm1=%mm1:~1,1%
if "%ss1:~0,1%"=="0" set ss1=%ss1:~1,1%
if "%cs1:~0,1%"=="0" set cs1=%cs1:~1,1%
set hh2=%time2:~0,2%
set mm2=%time2:~3,2%
set ss2=%time2:~6,2%
set cs2=%time2:~9,2%
if "%hh2:~0,1%"==" " set hh2=%hh2:~1,1%
if "%mm2:~0,1%"=="0" set mm2=%mm2:~1,1%
if "%ss2:~0,1%"=="0" set ss2=%ss2:~1,1%
if "%cs2:~0,1%"=="0" set cs2=%cs2:~1,1%
set /a msec1=3600000*%hh1%+60000*%mm1%+1000*%ss1%+10*%cs1%
set /a msec2=3600000*%hh2%+60000*%mm2%+1000*%ss2%+10*%cs2%
set /a elapsed=%msec2%-%msec1%
echo %elapsed%


How to use it...


Code: [Select]@echo off
set time1=%time%

echo time1 %time1%

rem ****** code to be timed here ***********
sleep 1

set time2=%time%

echo time2 %time2%

for /f %%A in ('elapsed-calc.bat "%time1%" "%time2%"') do set elapsed=%%A

echo elapsed time %elapsed% msec
Okay I think this will do it for seconds.
Code: [Select]set time1=%start%%~1
set time2=%finish%%~2
set hh1=%time1:~0,2%
set mm1=%time1:~3,2%
set ss1=%time1:~6,2%
if "%hh1:~0,1%"==" " set hh1=%hh1:~1,1%
if "%mm1:~0,1%"=="0" set mm1=%mm1:~1,1%
if "%ss1:~0,1%"=="0" set ss1=%ss1:~1,1%
set hh2=%time2:~0,2%
set mm2=%time2:~3,2%
set ss2=%time2:~6,2%
if "%hh2:~0,1%"==" " set hh2=%hh2:~1,1%
if "%mm2:~0,1%"=="0" set mm2=%mm2:~1,1%
if "%ss2:~0,1%"=="0" set ss2=%ss2:~1,1%
set /a msec1=3600000*%hh1%+60000*%mm1%+1000*%ss1%
set /a msec2=3600000*%hh2%+60000*%mm2%+1000*%ss2%
set /a elapsed=%msec2%-%msec1%
set /a length=%elapsed%/1000
echo %length%
Quote from: Jacob on December 22, 2008, 01:35:50 PM

Okay I think this will do it for seconds.
Code: [Select]set time1=%start%%~1
set time2=%finish%%~2
^^^
this doesn't look right
Quote from: Dias de verano on December 22, 2008, 03:21:04 PM
Quote from: Jacob on December 22, 2008, 01:35:50 PM
Okay I think this will do it for seconds.
Code: [Select]set time1=%start%%~1
set time2=%finish%%~2
^^^
this doesn't look right

It seems to work, as far as I am concerned. But it may not be correct for other uses.It "works" because the parameters %1 and %2 do not exist.

Better is this code.

Code: [Select]set time1=%start%
set time2=%finish%
Also, I do not think you realise that you have destroyed the accuracy of my code? Because you are dividing the millisecond figure by 1000, and because batch arithmetic is integer only, that means that 3999 msec would become 3 seconds for example.


I have adjusted the code so that it does not calculate milliseconds, but just seconds. The division is to get rid of the 3, 0's after the time for instance it outputs:
7000.
But yes, it is still inaccurate, but that is fine for the purpose that I am using it for. Thanks for all your help.Quote from: Jacob on December 23, 2008, 02:47:54 AM
I have adjusted the code so that it does not calculate milliseconds, but just seconds. The division is to get rid of the 3, 0's after the time for instance it outputs:
7000.
But yes, it is still inaccurate, but that is fine for the purpose that I am using it for. Thanks for all your help.

Well, why don't you do it properly, by reducing these multipliers by 1000:


like this...

Code: [Select]set /a sec1=3600*%hh1%+60*%mm1%+%ss1%
set /a sec2=3600*%hh2%+60*%mm2%+%ss2%
Then you don't need to divide by 1000.

Also, you do know the code will break if the start time is before midnight and the finish time is after? (And therefore measured time interval must be less than 24 hrs)

Works perfectly, thanks ever so much, and about the midnight glitch, its fine.


Discussion

No Comment Found