VBEval Timer function to measure elapsed time

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
bnc1
Pro Scripter
Posts: 127
Joined: Sun Jul 31, 2005 5:10 pm

VBEval Timer function to measure elapsed time

Post by bnc1 » Sun Jul 06, 2008 11:08 am

I have seen this code mentioned many times to measure elapsed time in seconds ...

VBSTART
VBEND
VBEval>Timer,startSeconds

..do some long running process

VBEval>Timer-%startSeconds%,elapsedSeconds

The variable elapsedSeconds measures the time the long running process took. However, if the process starts before midnight and ends after midnight the next day, you will get a negative number for elapsedSeconds since the VBEval Timer function measures seconds after midnight. A simple fix would be to add 24hrs (86400 seconds) to the elapsedSeconds variable, assuming the long running process is less than 24 hours in length :D

If>elapsedSecondselapsedSeconds=elapsedSeconds+86400
endif

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Fri Jul 11, 2008 9:52 am

Very helpful thanks.

And here's my code (including that tip) which evaluates the whole thing into hours minutes and seconds.

The subroutine gettimefromsecs can be called from anywhere where you have a variable elapsedSeconds which contains a number of seconds:

Code: Select all

// gets decimal place character so it will work internationally
LibFunc>kernel32,GetNumberFormatA,r,0,0,str:num,0,buf,255
MidStr>r_5,2,1,DecimalPlaceHolder

// Actual Subroutine
SRT>gettimefromsecs
If>elapsedSeconds<0
Let>elapsedSeconds=elapsedSeconds+86400
endif
let>seconds={round(%elapsedSeconds%)}
IF>seconds>0

let>hours=00
REPEAT>gethours
IF>seconds>3599
let>hours=hours+1
let>seconds=seconds-3600
ENDIF
let>gethours=seconds
StringReplace>gethours,DecimalPlaceHolder,.,gethours
UNTIL>gethours<3600

let>minutes=00
REPEAT>getminutes
IF>seconds>59
let>minutes=minutes+1
let>seconds=seconds-60
ENDIF
let>getminutes=seconds
StringReplace>getminutes,DecimalPlaceHolder,.,getminutes
UNTIL>getminutes<60

ENDIF
END>gettimefromsecs
This will output 3 variables

hours
minutes
seconds


So you can for example:

Code: Select all

msg>%hours%h : %minutes%m : %seconds%s
Will display (example) 10h : 51m : 26s

There may well be much better way to do this. If so feel free to let us know.

:-)
Phil Pendlebury - Linktree

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Mon Jul 14, 2008 1:48 pm

Ooops,

Added the:

Code: Select all

LibFunc>kernel32,GetNumberFormatA,r,0,0,str:num,0,buf,255
MidStr>r_5,2,1,DecimalPlaceHolder

So the above script will now actually work.

Sorry about that.

:oops:
Phil Pendlebury - Linktree

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts