|
-
Jun 15th, 2007, 04:38 PM
#1
[RESOLVED] Timer() vs GetTickCount()
I've seen it asserted many times on the boards that GetTickCount() is more accurate than Timer(), but frankly I don't see it.
Just now I tossed in a sample check of GetTickCount() into my sorting program (linked in sig) to see how they compare, and set it to benchmark sorting an array with only two (Long) elements using bubblesort.
The results:
Timer: 0.00078 seconds
GetTickCount: 0 milliseconds
Seems to me that Timer() is the clear winner. Am I missing something?
-
Jun 15th, 2007, 04:54 PM
#2
Re: Timer() vs GetTickCount()
Code:
Private Sub Command1_Click()
Dim lngTickA As Long
Dim lngTickB As Long
Dim sngTimerA As Single
Dim sngTimerB As Single
Dim i As Long
lngTickA = GetTickCount
Debug.Print lngTickA
For i = 0 To 9
Do
lngTickB = GetTickCount
Loop While lngTickB = lngTickA
Debug.Print lngTickB, lngTickB - lngTickA
lngTickA = lngTickB
Next i
Debug.Print
sngTimerA = Timer
Debug.Print sngTimerA
For i = 0 To 9
Do
sngTimerB = Timer
Loop While sngTimerB = sngTimerA
Debug.Print sngTimerB, sngTimerB - sngTimerA
sngTimerA = sngTimerB
Next i
End Sub
Code:
4243671
4243687 16
4243703 16
4243718 15
4243734 16
4243750 16
4243765 15
4243781 16
4243796 15
4243812 16
4243828 16
64049.06
64049.08 0.015625
64049.09 0.015625
64049.11 0.015625
64049.13 0.015625
64049.14 0.015625
64049.16 0.015625
64049.17 0.015625
64049.19 0.015625
64049.2 0.015625
64049.22 0.015625
It should be quite clear that both functions are accurate to 1/64 second.
-
Jun 15th, 2007, 05:25 PM
#3
Re: Timer() vs GetTickCount()
Wow, great post Logophobic. Took me a bit to grok what your code proves, but yeah, that's pretty irrefutable.
I wonder why the belief is so pervasive that GetTickCount() is more accurate? Probably nothing more than pro-API anti-VB bias.
Anyone know any way to get accurate timing results with a precision of a single millisecond or less?
-
Jun 15th, 2007, 05:35 PM
#4
Re: Timer() vs GetTickCount()
You can use the QueryPerformanceFrequency and QueryPerformanceCounter APIs. Merri has posted code for these functions wrapped up in a nice little class module. link to post
-
Jun 15th, 2007, 05:38 PM
#5
Re: Timer() vs GetTickCount()
-
Jun 16th, 2008, 05:20 PM
#6
New Member
Re: Timer() vs GetTickCount()
I wonder if there is a problem right there in your Dimension statements. TimerA has to be a Single and Tickcount uses longs in order for timer to be of an effective precision. I see that long and single both use 4 bytes, but is 'single' access any slower than Long? The only reason I might think so is that it may somehow take time for some sort of scientific notation check through which the single has to go that the long might not have to endure. Let me know if you know the two data types to be the same speed, I'm always interested in that sort of stuff!
 Originally Posted by Logophobic
Code:
Private Sub Command1_Click()
Dim lngTickA As Long
Dim lngTickB As Long
Dim sngTimerA As Single
Dim sngTimerB As Single
Dim i As Long
It should be quite clear that both functions are accurate to 1/64 second.
-
Jun 16th, 2008, 06:00 PM
#7
Re: [RESOLVED] Timer() vs GetTickCount()
Single is slower than Long because floating-point types are slower than integer types. This fact isn't relavent here, as both loops run thousands of cycles before a change is detected. The GetTickCount call seems to be roughly 15 to 16 times faster than the Timer call, but again, this is irrelavent.
Scientific notation is just a way of representing a number in string format, and has nothing to do with numeric data types.
-
Jun 16th, 2008, 07:10 PM
#8
New Member
Re: [RESOLVED] Timer() vs GetTickCount()
Dear Logophobic,
Why do you say that GetTickCount being fifteen times faster is irrelavent? That is the point of the original post, right? OHHH! I just took time to re-read the post and now see that precision was the issue. But, my first question remains.
Aaron Laws
 Originally Posted by Logophobic
Single is slower than Long because floating-point types are slower than integer types. This fact isn't relavent here, as both loops run thousands of cycles before a change is detected. The GetTickCount call seems to be roughly 15 to 16 times faster than the Timer call, but again, this is irrelavent.
Scientific notation is just a way of representing a number in string format, and has nothing to do with numeric data types.
-
Jun 16th, 2008, 07:20 PM
#9
Re: [RESOLVED] Timer() vs GetTickCount()
If you're making timing in a loop, it would be insane to keep either one of the calls in the tightest loop, both would slow down the code dramatically (of course Timer would slow down it more noticeably i that case). When placed in a less critical position the net effect of the function call's speed becomes close to irrelevant, so it is more of a matter of taste which one is used.
Code:
Do
' loose loop
Do
' tight loop doing some very fast math
' = stupid place to put Timer, GetTickCount or DoEvents
Loop
' loose loop
' = a much better place to put Timer, GetTickCount or DoEvents
Loop
-
Jun 17th, 2008, 07:26 AM
#10
Re: [RESOLVED] Timer() vs GetTickCount()
 Originally Posted by Merri
If you're making timing in a loop, it would be insane to keep either one of the calls in the tightest loop, both would slow down the code dramatically (of course Timer would slow down it more noticeably i that case). When placed in a less critical position the net effect of the function call's speed becomes close to irrelevant, so it is more of a matter of taste which one is used.
Exactly. Nobody cares how long one loop iteration takes; the relevant info is how long the entire loop takes.
Timing such a thing only requires two timing calls, whether they be Timer(), GetTickCount(), or QueryPerformance(): One to start the timer and one to stop.
This thread was a reaction to seeing a lot of recommendations to use GetTickCount() instead of Timer() because it's more accurate. That's clearly not true; you have to use QueryPeformance to enhance timing precision.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|