Jump to content

Calculated Runtime is lesser than actual Runtime


CT83
 Share

Recommended Posts

I am using the code given below to calculate the runtime but, the run time returned is sometimes, slightly lesser (the runtime field on my gui runs is slightly faster than the normal clock) How is this possible? 

#include <Date.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

Global $iFirst_Run=0
Global $iS_Time=0, $iS_TimeDisp=0

;test
Calc_Runtime(0)
Sleep(5000)
$test=Calc_Runtime(0)
MsgBox("","",$test)
$test=Calc_Runtime(1)
MsgBox("","",$test)

Func Calc_Runtime($choice)
    If $iFirst_Run=0 Then
    $iS_TimeDisp = _NowTime(5)
    $iS_Time = Number(_StringStripChars($iS_TimeDisp, ':'))
    $iFirst_Run=1
    EndIf
Local $iTime_Now=Number(_StringStripChars(_NowTime(5), ':'))
Local $iR_Time= $iTime_Now - $iS_Time

Switch $choice
;1 is for returning in Integer;  0 is for proper display time
    Case 0
        Return $iR_Time
    Case 1
        Return Sec2Time($iR_Time)
EndSwitch
EndFunc

Func _StringStripChars($sString, $sChars)
    If $sChars == '' Then Return SetError(1, 0, $sString)
    If $sString == '' Then Return SetError(2, 0, $sString)
    $sChars = StringRegExpReplace($sChars, '\\([eEqQ])', '\1\\')
    Return StringRegExpReplace($sString, '[\Q' & $sChars & '\E]', '')
EndFunc

Func Sec2Time($nr_sec)
$nr_sec=$nr_sec
$sec2time_hour = Int($nr_sec / 3600)
$sec2time_min = Int(($nr_sec - $sec2time_hour * 3600) / 60)
$sec2time_sec = $nr_sec - $sec2time_hour * 3600 - $sec2time_min * 60
Return StringFormat('%02d:%02d:%02d', $sec2time_hour, $sec2time_min, $sec2time_sec)
EndFunc   ;==>Sec2Time

 

Link to comment
Share on other sites

  • Moderators

CT83,

I imagine there are rounding errors as you are only comparing seconds. And that is a rather roundabout way of determining the runtime - I would do something like this:

#include <Date.au3>
#include <MsgBoxConstants.au3>

Global $iFirst_Run = 0
Global $iS_Time = 0, $iS_TimeDisp = 0

;test
Calc_Runtime(0)
Sleep(5000)
$test = Calc_Runtime(0)
MsgBox($MB_SYSTEMMODAL, "", $test)
$test = Calc_Runtime(1)
MsgBox($MB_SYSTEMMODAL, "", $test)

Func Calc_Runtime($choice)
    If $iFirst_Run = 0 Then
        $iS_Time = TimerInit()
        $iFirst_Run = 1
    EndIf
    Local $iR_Time = TimerDiff( $iS_Time)
    Switch $choice
        ; 0 is for returning in Integer;  1 is for proper display time
        Case 0
            Return Int($iR_Time / 1000)
        Case 1
            Local $iHours, $iMins, $iSecs
            _TicksToTime($iR_Time, $iHours, $iMins, $iSecs)
            Return StringFormat("%02i:%02i:%02i", $iHours, $iMins, $iSecs)
    EndSwitch
EndFunc   ;==>Calc_Runtime

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

19 hours ago, Melba23 said:

CT83,

I imagine there are rounding errors as you are only comparing seconds. And that is a rather roundabout way of determining the runtime - I would do something like this:

#include <Date.au3>
#include <MsgBoxConstants.au3>

Global $iFirst_Run = 0
Global $iS_Time = 0, $iS_TimeDisp = 0

;test
Calc_Runtime(0)
Sleep(5000)
$test = Calc_Runtime(0)
MsgBox($MB_SYSTEMMODAL, "", $test)
$test = Calc_Runtime(1)
MsgBox($MB_SYSTEMMODAL, "", $test)

Func Calc_Runtime($choice)
    If $iFirst_Run = 0 Then
        $iS_Time = TimerInit()
        $iFirst_Run = 1
    EndIf
    Local $iR_Time = TimerDiff( $iS_Time)
    Switch $choice
        ; 0 is for returning in Integer;  1 is for proper display time
        Case 0
            Return Int($iR_Time / 1000)
        Case 1
            Local $iHours, $iMins, $iSecs
            _TicksToTime($iR_Time, $iHours, $iMins, $iSecs)
            Return StringFormat("%02i:%02i:%02i", $iHours, $iMins, $iSecs)
    EndSwitch
EndFunc   ;==>Calc_Runtime

M23

Thanks! It worked! You are awesome!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...