Re: I need to get the lapsed time in the execution of various functions...



"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





.



Relevant Pages

  • Re: now.ticks does not work
    ... Using it to time code that is faster then the quantum that is used ... For more details on the Precision & Resolution of Now.Ticks, ... | i will check out your QueryPerformanceCounter ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Constructing a more precise "Now()" and "DateDiff()"
    ... However, if you actually do want to use a "timestamp" and if the resolution of my own suggested method of using GetLocalTime is not sufficient for you then why not just record the actual time at the start of your program. ... Then every time you want to know the current time you simply use the timeGetTime function and deduct from its returned value the initial value that you got from timeGetTime at the start of your program. ... The resolution of timeGetTime is one millisecond. ... if the one millisecond resolution of timeGetTime is still not sufficient for your purposes you can instead use QueryPerformanceCounter in a similar way. ...
    (microsoft.public.vb.general.discussion)
  • Re: now.ticks does not work
    ... you should use Long or DateTime to store T. ... DateTime has a "resolution" of 100-nanoseconds, its precession can vary, in ... "high-resolution performance counter", which I understand is a special hard ... Use QueryPerformanceCounter to Time code in VB.NET: ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Height of text box in detail section
    ... Are you saying that the API ... the desired printer in order to set the DrawWidth property accurately. ... ' *** START ORIGINAL POST ... will have to factor in the output device resolution. ...
    (microsoft.public.access.reports)
  • Re: Fastest time counter
    ... I don't see any bugs. ... How do you know the counter is losing resolution? ... What's exactly is wrong with QueryPerformanceCounter to begin with? ... If you're wanting high resolution timing then the QueryPerformanceXXX ...
    (microsoft.public.dotnet.framework.performance)