d4500 opened this issue on Jun 14, 2003 ยท 28 posts
lmckenzie posted Tue, 17 June 2003 at 7:07 AM
Attached Link: http://www.mvps.org/ccrp
I kept getting wildly different results from basically the same code until I realized that you must not be typing your variables. By default in VB, you get variants which take up 16 bytes each as opposed to 4 bytes for a long integer or a single. There is also overhead for doing type conversion from a variant to the desired type. The difference is quite dramatic. I get sub 200 Millisecond times (averaging 10 iterations). The code below uses a hi resolution timer object you can get at the link. I also tested using the timer function as you did, which yields slightly faster results but probably not as accurate. This doesn't invalidate your relative performance numbers but it shows why you always want to use typed variables. 155 Milliseconds vs 2.8 Seconds. I did turn on all the compiler optimizations for this executable. Not using any optimizations is only slightly slower - a few milliseconds. '--------------------------------------------------------- Dim objStopWatch As ccrpStopWatch Dim lngCount As Long Dim intIterations As Integer Dim lngTotalTime As Long Dim lngAvgMs As Long Dim strResults As String Me.Caption = "HiRez Timer 10 Iterations" Set objStopWatch = New ccrpStopWatch For intIterations = 1 To 10 objStopWatch.Reset For lngCount = 1 To 50000000 Next lngCount lngTotalTime = lngTotalTime + objStopWatch.Elapsed Next intIterations lngAvgMs = lngTotalTime / 10 strResults = "lngCount: " & lngCount - 1 & vbCrLf strResults = strResults & "Total Time: " & lngTotalTime & " Milliseconds" strResults = strResults & vbCrLf & "Avg. Time: " & lngAvgMs strResults = strResults & " Milliseconds (" & (lngAvgMs / 1000) & " Seconds)" lblResults.Caption = strResults Set objStopWatch = Nothing '---------------------------------------------------------"Democracy is a pathetic belief in the collective wisdom of individual ignorance." - H. L. Mencken