Re: I need to get the lapsed time in the execution of various functions...
- From: "Mike Williams" <Mike@xxxxxxxxxxxxxxxxx>
- Date: Wed, 26 Jul 2006 10:00:30 +0100
"Carlos Villaseñor M." <cvmdiseno@xxxxxxxxxxx> wrote in message news:ebvkiWIsGHA.3684@xxxxxxxxxxxxxxxxxxxxxxx
Public Sub Benchmark_02()
t_1 = Now() <---------------- Is this right? Is this accurate?
Nope. Its not very accurate at all. Most other timing functions also have a fairly poor resolution (VB Timer function, GetTickCount API, etc, etc). If you want timings with a greater resolution you can use the timeGetTime API, which has a resolution of 1 millisecond provided that you use the timeBeginPeriod API to set it to 1 millisecond before using it. For timings with an even higher resolution you can use the QueryPerformanceCounter API. On most machines this is capable of giving you timings with a resolution of about 1 microsecond (although there are some "catches" to be aware of where under certain unusual circumstances the QueryPerformanceCounter API can "jump" by a significant amount. These errors are so large though in relation to the timing you are likely to be using it for that they are easy to detect. Try the following example. There is a very slight inaccuracy in this simple example because the code does not take into account the time required for the QueryPerformance API to do its work, but on most machines this is only about a microsecond so it shouldn't be a problem (although you can add code to account for it if you wish). I seem to recall there being another little problem with this API under certain special circumstances, but I'm not sure whether they apply to your own system. Something to do with the timer reading the clock pulses from a different source on muiltiprocessor machines and especially on laptops which are capable of "clocking down" under certain circumstances (although I think that problem occurs only on multiprocessor laptops). Maybe someone else here can give you more information. Anyway, here's the code which I think you should find suitable for your own purposes:
Mike - MVB Visual Basic
Option Explicit
Private Declare Function QueryPerformanceFrequency _
Lib "kernel32" (lpFrequency As Currency) As Long
Private Declare Function QueryPerformanceCounter _
Lib "kernel32" (lpPerformanceCount As Currency) As Long
Private fullwidth As Long, fullheight As Long
Private Sub Command1_Click()
Dim frequency As Currency
Dim startTime As Currency
Dim endTime As Currency
Dim result As Double
If QueryPerformanceFrequency(frequency) = 0 Then
Exit Sub ' system doesn't support performance counter
End If
QueryPerformanceCounter startTime
' place the code to time here
' (simple For Next loop is just an example)
Dim n As Long
For n = 1 To 1000
Next n
'
QueryPerformanceCounter endTime
result = CDbl(endTime - startTime) / frequency
result = CLng(result * 1000000)
Print result & " microseconds"
End Sub
.
- References:
- VB6: I need to get the lapsed time in the execution of various functions...
- From: Carlos Villaseņor M.
- VB6: I need to get the lapsed time in the execution of various functions...
- Prev by Date: Re: How to get the machine id / Hard disk id
- Next by Date: Re: I need to get the lapsed time in the execution of various functions...
- Previous by thread: Re: VB6: I need to get the lapsed time in the execution of various functions...
- Next by thread: Re: I need to get the lapsed time in the execution of various functions...
- Index(es):
Relevant Pages
|