Page 1 of 1
ElapsedMilliseconds() vs GetTickCount_()
Posted: Tue May 10, 2016 3:11 am
by Damion12
I was to believe ElapsedMilliseconds() was alias for windows api -- but it not seem so.
Code: Select all
Debug ElapsedMilliseconds()
Debug GetTickCount_()
Give close results, but not same.
More problematic, and this is just to show problem, is that GetTickCount_() seems to not actually count the milliseconds.
What is difference between to api call and pb function to get different results?
Code: Select all
; GetTickCount_() results in timing less than sleep/delay
For x = 1 To 10
a = GetTickCount_()
Delay(3241) ; or use Sleep_(3241) -- result is the same
Sleep_(3241)
b = GetTickCount_()
Debug b-a ; this answer (for me) is 3234 or 3235 or 3250 (usually less than 3241)
Next
Debug "----------------------"
; this almost always right answer
For x = 1 To 10
a = ElapsedMilliseconds()
Delay(3241)
b = ElapsedMilliseconds()
Debug d-c ; this answer is almost always 3241, occasionally 3242
Next
Re: ElapsedMilliseconds() vs GetTickCount_()
Posted: Tue May 10, 2016 5:39 am
by Keya
i dont know about Linux or OSX, but on Windows the ElapsedMilliseconds() function seems to be a dual wrapper... it tries the QueryPerformanceFrequency/Counter method first (which i think is more accurate?), and only if that fails it uses GetTickCount() as a fallback, which is why their values are probably different. You should be able to do the same timings with either as long as you use the same method for both Time1 and Time2
Re: ElapsedMilliseconds() vs GetTickCount_()
Posted: Tue May 10, 2016 8:45 am
by Dude
Damion12 wrote:GetTickCount_() seems to not actually count the milliseconds.
It does count them, but from MSDN:
"The resolution of the GetTickCount function is limited to the resolution of the system timer, which is typically in the range of 10 milliseconds to 16 milliseconds."
So the count might be out from 10-16 ms per consecutive call. This means it's a Windows API issue, not a PureBasic issue.
Re: ElapsedMilliseconds() vs GetTickCount_()
Posted: Tue May 10, 2016 2:38 pm
by Damion12
Dude wrote:Damion12 wrote:This means it's a Windows API issue, not a PureBasic issue.
Never said it was purebasic problem! Just want to understand why the difference.
Re: ElapsedMilliseconds() vs GetTickCount_()
Posted: Tue May 10, 2016 2:43 pm
by Damion12
Keya wrote:ElapsedMilliseconds() function seems to be a dual wrapper... it tries the QueryPerformanceFrequency/Counter and only if that fails it uses GetTickCount() as a fallback
/quote]
I will try using QueryPerformanceCounter to see if that is ok;
Keya wrote:You should be able to do the same timings with either as long as you use the same method for both Time1 and Time2
You would think that an api that might use one or the other would be the one to return varying results, I am trying to get better time count (and actually using 64bit get tick count so I don't wrap around)
Thank you - I look at QueryPerformanceCOunter now -- but I I read (
forums here) that it is not good to use that .
Re: ElapsedMilliseconds() vs GetTickCount_()
Posted: Tue May 10, 2016 3:29 pm
by Damion12
Is interesting results -- they seem to be consitient in that elapsedmillisecond is best and QueryPerformanceCounter() and GetTickCount() be different by similar amount.
I not blame pb -- i just try to find best way to time things w/o variation; my problem not this exact thing; I just write this to show variations exist.
Results from code below
Number of values QueryPerformanceCounter: 5
Number of values GetTickCount returned: 4
Number of values ElapseMillisecond returned: 2
Code: Select all
#loopCount = 100
Procedure.q Qtimer()
Static.q freq
Protected.q counts
If maxfreq=0
QueryPerformanceFrequency_(@freq)
freq /1000
EndIf
QueryPerformanceCounter_(@counts)
ProcedureReturn (counts/freq)
EndProcedure
Define.q a,b
Define.i x
Define NewMap returnValues.q()
For x = 1 To #loopCount
a=Qtimer()
Delay(3241)
b=qTimer()
returnValues(Hex(b-a)); : Debug MapKey(returnValues())
Next
Debug "Number of values QueryPerformanceCounter: "+MapSize(returnValues())
ClearMap(returnValues())
For x = 1 To #loopCount
a=GetTickCount_()
Delay(3241)
b=GetTickCount_()
returnValues(Hex(b-a)) ;: Debug MapKey(returnValues())
Next
Debug "Number of values GetTickCount returned: "+MapSize(returnValues())
ClearMap(returnValues())
For x = 1 To #loopCount
a=ElapsedMilliseconds()
Delay(3241)
b=ElapsedMilliseconds()
returnValues(Hex(b-a)) ;: Debug MapKey(returnValues())
Next
Debug "Number of values ElapseMillisecond returned: "+MapSize(returnValues())
Re: ElapsedMilliseconds() vs GetTickCount_()
Posted: Wed May 11, 2016 3:31 am
by Dude
Damion12 wrote:Never said it was purebasic problem!
I never said you did! (You quoted the wrong person).