PDA

View Full Version : GetTickCount



zlatkoAB
26-02-2011, 14:12
--------------------------------------------------------------------- Hi Zlatko, if you think you find a problem with ThinBASIC, please report the issue along with source code on ThinBASIC forum, thank you very much! -------------------------------------------------------------------- Petr wrote on basicprogramming.org Hi Petr...ok im here. Im not sure maby you right. I first time test from my own editor and i think maby is something wrong with editor. then i try with thinAir and same results. So sometimes i recive result and sometimes not. As i say result on my old 1.6Ghz pentium is about 10msec. i think that this result is very good. Cretive 20 msec Dlib 20 msec here is code: ------------------------------------------------------------ Dim start As Long Dim over As Long Dim time As Long Dim times As String Dim i As Long start = 0 over = 0 time = 0 start = GetTickCount ' empty loop 10000 iterations For i = 0 To 20000 Next over = GetTickCount time = over - start times = str$(time) MsgBox 0,times,%MB_OK,"Time in miliseconds" time = 0 '---------------------------------------------------------- Is this code right?

Petr Schreiber
26-02-2011, 14:35
Hi Zlatko,

thanks for making the report.

I transformed the code to more readable form:


Dim start, over, time As DWord
Dim times As String

Dim i As Long

start = GetTickCount
' empty Loop 10000 iterations
For i = 0 To 10000
Next
over = GetTickCount

time = over - start
times = Str$(time)

MsgBox(0, times, %MB_OK, "Time in miliseconds")


On my PC, it returns 0. It is not a bug, but a well known limitation of the GetTickCount function from Win32 - its precision is simply low, as says 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.

Source: http://msdn.microsoft.com/en-us/library/ms724408.aspx

When you need to measure such a tiny time deltas, it is better to use something with microseconds resolution. This is done via QueryPerformanceCounter/QueryPerformanceFrequency, which is implemented as hiResTimer in ThinBASIC.

Your code would then become:


Dim start, over As Quad
Dim time As Ext
Dim times As String

Dim i As Long

HiResTimer_Init

start = HiResTimer_Get
' empty Loop 10000 iterations
For i = 0 To 10000
Next
over = HiResTimer_Get

time = (over - start)/1000 ' microseconds / 1000 = milliseconds
times = Format$(time, "#.000")+"ms"

MsgBox(0, times, %MB_OK, "Time in miliseconds")


On my PC it is about .600ms (that is less than one millisecond). So now you can see, why GetTickCount, with resolution of tens of milliseconds cannot measure something which takes less than one millisecond.

I hope it is clear, if not, then let me know and I will try to explain better.

Thanks a lot for bringing up this issue to our eyes, I think it will be good to add this info to help file.


Petr

zlatkoAB
26-02-2011, 14:58
Yes Petr you right about precission of GetTickCount.
I try your new code and i recive 1.284 msec which is great result.
I wonder to ,how guy on basicprogramming made his tests???
Also i will try to measure for/next loop when we comes to GUI area.
I mean add edit control which show iteration index inside edit
control and how much time need for this operation.

Michael Clease
27-02-2011, 01:46
Petr your example could be seen as confusing unless you use the correct unit, µs or ms, at the moment you are showing a microsecond result and then saying it is in milliseconds.

:onthequiet:

Petr Schreiber
27-02-2011, 11:47
Hi Mike,

I think my example is correct . Let me explain:


Values returned by HiResTimer_Get are in µs.
I subtract values in µs but then I divide result by 1000 -> by this I get value in ms

time = (over - start)/1000
Then I display result in ms as fractional number:

times = Format$(time, "#.000")+"ms"
So if it displays 0.555ms, it should be the same as saying 555µs

Am I correct? I might be not, but I still think it makes sense. Let me know!


Petr