Dear Sir/Madam

How to check execution time of a procedure with the help of Timer control? How i have tried, i am attaching herewith my test program. Please guide me.

Dani AI

Generated

Good progress in the thread — your simple Timer-based approach is a perfectly fine quick check, and was right to point out that a UI Timer won’t behave while the main thread is busy. For reliable, high-resolution timing of a long procedure you want a timer that does not depend on the message pump. The Windows QueryPerformanceCounter API gives sub-millisecond precision and is straightforward to call from VB6.

Here is a compact, original example showing how to wrap QueryPerformanceCounter and use it to time a procedure:

' High-resolution timing (VB6)
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long

Private gPerfFreq As Currency

Private Sub Form_Load()
    QueryPerformanceFrequency gPerfFreq
End Sub

Private Function PerfNow() As Double
    Dim cnt As Currency
    QueryPerformanceCounter cnt
    PerfNow = CDbl(cnt) / CDbl(gPerfFreq)   ' seconds (high precision)
End Function

' Usage:
' Dim t0 As Double: t0 = PerfNow()
' ...run code...
' Debug.Print "Elapsed (s):"; PerfNow() - t0

Practical tips and gotchas: avoid updating a Label/ textbox each iteration — UI updates dominate timing. If you need user feedback, update the UI only every N iterations (for example every 1000). Calling DoEvents lets the Timer control and UI update but changes execution timing (and slows the loop). If you keep using the VB Timer function remember it wraps at midnight; protect against that if your task can span midnight. For full responsiveness without skewed timings, move heavy work to an out‑of‑process worker (ActiveX EXE or separate process) or use a profiler tool when you need per-procedure breakdowns.

Recommendation: use QueryPerformanceCounter for accurate elapsed times, minimize UI work during the timed section, and use UI updates or DoEvents only for visible progress (as suggested) so measurements reflect the real work, not rendering overhead.

Recommended Answers

All 7 Replies

Have a look at the attached zip, way much easier method to get a 100% returned time. You can shorten the code by a lot when using it inside your loops.:)

Dear AndreRet,

As i have seen in the firt part of your program, result is showing the time of clicking inbetween Enable timer first(Cmd3) to

Stop Timer(cmd1) button.

In the second part, result is showing the same but here you have done summation of seris of clicking inbetween Enable timer

first(Cmd4) to Stop Timer(cmd2) button.

But i am not getting the result as i want. May be i am not getting the logic as you are thinking.

I think you are cleared what i want. However, i am once again putting my requirement.

I want to measure the time require by a long procedure from first line to last line that may be 1000 lines of codes or few

loop and elseif statement as i have puted a loop in my test program (1 To 1000000). I want to see the time how many seconds are taking by that procedure or other procedure

In my test program command1(First Test) button showing the result as starting and ending time of procedure. Where i can see the difference as 10 seconds in my PC (Pentium 4). And that test has been done without taking the help of timer.

In the Command2(second test) and Command3(third test), i have used timer but result has not got. And i have seen when i cliked on all three button one by one timers controls are pausing when commands are running.

Thanking you once again.

a million count loop will slow down your computer into an almost crawl. If you really want to show the status of a loop in time, use a progress bar. Remember that a user's pc might be slower or faster than your test machine, resulting in a lot of unwanted returned time results.:)

Also use DoEvents(), very useful if you have time consuming, cpu eating code since it will allow Windows to process the Applications messages and thus, your Application/Program will not freeze while the high cpu usage code is running. This will allow the user to continue to interact with your program while the cpu intensive task is running.

BUT unfortunately, there is a tradeoff if you use DoEvents(). That tradeoff is ‘Performance’. The more you call DoEvents() the higher the performance drop since Windows will have to check and use the cpu to process your apps messages/procedures and will take some cpu cycles away from your monster code. Thankfully, there IS a solution to ‘Partially’ offset the performance loss of using DoEvents() while ‘Still’ keeping the application from freezing.

'Under your command button - ...
Private Sub Command5_Click()

Timer1.Enabled = True

prg.Min = 0
prg.Max = 1000000
prg.Value = 0

Dim xLoop As Double

Do While xLoop <= 1000000
    If prg.Value >= 1000000 Then
    Timer1.Enabled = False
    Exit Do
        Else
    DoEvents
    xLoop = xLoop + 1
    prg.Value = prg.Value + 1
    End If
Loop
End Sub

'Under the timer - ...
Private Sub Timer1_Timer()

lblOnce.Caption = lblOnce.Caption + Timer1.Interval
End Sub

That should now solve your problem. You are giving the user some 'visible' input by means of the progressbar AND you can time the loop.:)

Dear AndreRet,

Sir, i have got another solution for checking execution time of a procedure. This time i have got the solution in fraction without using timer.

Private Sub Command1_Click() 'without timer its successfull
Dim ST As Double
Dim FT As Double
ST = Timer
For a = 1 To 1000000 Step 1
    Label1.Caption = Val(Label1.Caption) + 1
Next
FT = Timer
Text1.Text = "Total time required by this procedure is " + Str(FT - ST) + " Seconds"
End Sub
commented: Nice! +6

Cool. I hope you still have all your hair intact, and did not pull it out trying to get the solution. It is a very nice and simple solution, some kudo's for you.:)

Please mark this as solved, found at the bottom of this page, thanks.

Dear AndreRet,

Your information is valuable for me sir and thanks for being with me for solving this question. Still I am keeping all my hair in original state. It may be happen as you said for my other question :), because i am trying heard for getting solution.

Thank you once again for appreciating me.

No problem. You'll get through this, I promise.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.