Computer Hope

Microsoft => Microsoft DOS => Topic started by: g2c on March 11, 2015, 03:03:05 PM

Title: measuring elapsed time of a task evoked in a dos batch file
Post by: g2c on March 11, 2015, 03:03:05 PM
hello,

i am trying desperately to measure the duration of a task called within a dos batch loop running on a windows 8.1 64 bits and an seriously asking myself if this is at all possible

Thanks in advance for your help

Tried the procedure in http://stackoverflow.com/questions/9922 ... batch-file
the expression
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)

result in "Unbalanced parenthesis." whenever hour < 10

Tried the procedure in
http://www.computerhope.com/forum/index ... ic=78053.0

the expression in bold gives
"Invalid number. Numeric constants are either decimal (17), hexadecimal (0x11), or octal (021)."
whenever 8<seconds <10

echo start time=%TIME%
set HH=%TIME:~0,2%
echo Hour=%HH%
set MM=%TIME:~3,2%
echo minutes=%MM%
set /a MM=%MM% * 60
echo Total Seconds in given minutes=%MM%
set /a HH=%HH% * 3600
echo Total seconds in given Hours = %HH%
set SS=%TIME:~6,2%
echo seconds= %SS%
Set /a start=%HH% + %MM% + %SS%
echo start time in seconds =%start%
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: Squashman on March 11, 2015, 03:37:55 PM
http://www.dostips.com/forum/viewtopic.php?p=38387
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: g2c on March 11, 2015, 04:40:27 PM
thanks a lot, looks exactly what i need

I am ashamed to admit that i don't know how to use this procedure :(

Please help

Guy
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: Squashman on March 11, 2015, 06:57:25 PM
It shows you exactly how to use it.
Quote from: dbenham
getTimestap.bat can also be used to conveniently compute elapsed time of nearly any time interval, without worrying about the limits of batch arithmetic. This is extremely convenient for timing events.

Two calls to getTimestamp are used to store the current timestamp at the beginning and end of a process. The timestamps are expressed as milliseconds since midnight, January 1, 1970. Then one more call to getTimestamp is used to compute the interval. The date for the interval is specified as timestamp2 - timestamp1, and the format can provide the elapsed time in whatever units are required.
Code: [Select]
@echo off
setlocal

call getTimestamp -f {ums} -r t1
:: Some long running process here
call getTimestamp -f {ums} -r t2

:: This computes the elapsed time as decimal hours.
:: It supports both positive and negative intervals.
call getTimestamp -d %t2%-%t1% -f "{uhd} hours"

:: This computes the elapsed time as days, hours, mins, secs, ms
:: It only supports positive intervals
call getTimestamp -d %t2%-%t1% -f "{ud} days {hh}:{nn}:{ss}.{fff}" -u
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: patio on March 11, 2015, 07:19:52 PM
You can lead a horse...ahhh  nevermind.
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: Geek-9pm on March 11, 2015, 07:22:10 PM
What  is the objective?
It is not about prime lapse.
Time clasp does not require that much work.
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: Squashman on March 11, 2015, 07:25:11 PM
What  is the objective?
It is not about prime lapse.
Time clasp does not require that much work.
I have no idea what prime lapse or Time Clasp are.
All the user wants is to measure how much time it took to run a routine.  So you get the start time and the end time and subtract.

Dave Benham wrote a very powerful utility to do this.
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: g2c on March 12, 2015, 01:53:31 AM
Thanks Squashman, I saw this example but i don't know where should i put the routine definition: that of getTimestamp  code itself so that the commend interpreter knows where to search for the called code
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: Squashman on March 12, 2015, 05:10:12 AM
getTimeStamp is a 2nd batch file that will either need to exist in the same folder as your current batch file or in the SYSTEM PATH.

Code: [Select]
@echo off
setlocal

:: SETTING THE TIME STAMP BEFORE THE TASK
:: getTimeStamp is setting a variable %t1% as the start time
call getTimestamp -f {ums} -r t1
:: YOUR CODE GOES
:: DO A BUNCH OF CRAP HERE
:: BLAH BLAH BLAH BLAH BLAH

:: SETTING THE TIME STAMP AFTER THE TASK IS DONE.
:: getTimeStamp is setting a variable %t2% as the end time
call getTimestamp -f {ums} -r t2

:: This computes the elapsed time as decimal hours.
:: It supports both positive and negative intervals.
call getTimestamp -d %t2%-%t1% -f "{uhd} hours"

:: This computes the elapsed time as days, hours, mins, secs, ms
:: It only supports positive intervals
call getTimestamp -d %t2%-%t1% -f "{ud} days {hh}:{nn}:{ss}.{fff}" -u
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: g2c on March 12, 2015, 05:26:52 AM
Dear Squashman,

You don't know how much i owe you! I never spent so much time on something so trivia

Guy
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: Squashman on March 12, 2015, 06:12:32 AM
An easier method that you can embed in your batch file was posted on DosTips.com in the thread you started over there.
http://www.dostips.com/forum/viewtopic.php?p=40395#p40395
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: g2c on March 12, 2015, 06:58:13 AM
Funny, they sent me an email saying my post needs be inspected first and that I should be informed of the result of the investigation :)
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: Squashman on March 12, 2015, 07:05:32 AM
Funny, they sent me an email saying my post needs be inspected first and that I should be informed of the result of the investigation :)
Yes.  I am one of the moderators. But I turn of the notification saying that the post has been approved.  We only approve the first couple of posts.  If you want notifications for the thread, then subscribe to the topic.
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: g2c on March 12, 2015, 07:11:54 AM
The "simpler" one may exhibit a misbehaviour at midnight
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: Squashman on March 12, 2015, 07:27:55 AM
Correct. That is why I suggested using Dave's external batch to call out to.  But Antonio may have some extra code he can tack on for the rollover into another day.
Title: Re: measuring elapsed time of a task evoked in a dos batch file
Post by: Squashman on March 12, 2015, 07:33:18 AM
Antonio posted a 2nd link that talks about the 24 hour problem.
Dave just added one line of code.
http://www.dostips.com/forum/viewtopic.php?f=3&t=6271
Quote from: miskox
What would be the best solution if I start the process before midnight and when time changes from 23:xx:xx to 00:xx:00 to have a correct time difference? Maybe adding 24 to hours? Or maybe adding dates to start time and end time?

Saso
Quote from: dbenham
Yes, assuming your process takes less than 24 hours to complete, then all you need to do is conditionally at 24 hours if elapsed is less than 0.
Code: [Select]
if %elapsed% lss 0 set /a elapsed+=24*60*60*100

Dave Benham