Dear Sir/Madam,

I have two types of procedure for one/same result. No. 1 procedure is very long without loop and no. 2 procedure is sort with many loops. So I want to check those procedure, which one is taking less time than another.

Please guide me.

Dani AI

Generated

Short summary and practical fixes for timing VB6 procedures (for and others): the simple “print system time before/after” approach that suggested is fine and often the easiest. The reason your Timer control stopped updating during the long procedure (what tried to use) is that VB6 Timer events run on the same UI/message thread as your code; a long-running procedure blocks the message loop, so the Timer event and label updates don’t execute until your procedure finishes.

A reliable, low-effort method is to sample a millisecond clock before and after the code under test with the Win32 API GetTickCount. It avoids UI timers and gives straightforward millisecond results:

Private Declare Function GetTickCount Lib "kernel32" () As Long

Dim t0 As Long, t1 As Long
t0 = GetTickCount()
Call ProcedureToTest
t1 = GetTickCount()
Debug.Print "Elapsed ms: " & (t1 - t0)

To reduce noise, run each procedure many times and average (or take the median). For sub-millisecond precision, use QueryPerformanceCounter / QueryPerformanceFrequency (higher resolution), but GetTickCount is usually sufficient for procedures that take milliseconds or longer.

Practical tips when comparing two implementations:

  • Use identical inputs and a realistic dataset (small inputs can hide differences).
  • Run each test several dozen or hundred times and discard the first run (JIT, cache warm-up).
  • Avoid updating the UI inside timed code — updating labels/cards changes the timing.
  • Don’t rely on Timer controls to measure work done on the UI thread; they’re for keeping UI responsive only if the UI thread yields (e.g., using DoEvents, which itself affects timing and can introduce reentrancy).
  • If you need both responsiveness and accurate timing, run heavy work off the UI thread (ActiveX EXE or a native DLL/threading approach) and measure inside that worker.

These steps will give consistent, repeatable comparisons between your two calendar-generation approaches.

Recommended Answers

All 10 Replies

Execution time is independent of lines of code.
It may happen that a procedure has 1000 lines of code because of a long running IF ELSE but the line 10 to 1000 never gets executed.

It depends on what the code is actually doing.

Execution time is independent of lines of code.
It may happen that a procedure has 1000 lines of code because of a long running IF ELSE but the line 10 to 1000 never gets executed.

It depends on what the code is actually doing.

Thanks for replying, but i need the method or technique how to check the time required for those procedure.

Add a timer 1 and 2 to your form. In your loop add a time factor to a label, something like -

'Your loop started...
Label1.Caption = Label1.Caption + Timer1.Interval
Timer1.Enabled = False
Timer1.Enabled = True

Simply print system time at the beginning of the Procedure (before any looping or conditional branching) and once again at the end . and check the difference.

Dear AndreRet,

Thanks for your reply. I have tried with timer earlier also but that was not worked. Anyway thanks for your reply.

Dear Debasisdas,

Thanks for your simple but excellent logic which has worked for me. And for your kind information both the procedure has taken same time.

Thank you all

As i have said earlier , that would depend on what the procedure is doing.

for example if your procedure is doing some data processing test it against Hugh data, definitely you will get some difference in execution time.

I agree with Das, hence my first suggestion with timers where you can get an interval of 0.001 seconds. a Normal 1 record data retrieval normally returns within 0.009 seconds depending on your system, network, database etc, which by using the system time will show as the same time for both tests. If comparing huge databases, it might work, but you still will not get a 100% returned value.:)

My 2 cents worth.:)

Dear AndreRed,

I have a program to make a Calendar on Excel Sheet. Where Calendar year will be chosen by user and giving his own/office holiday list.

On that program the main procedure is taking few time for fully executing. I have tried as Dbasisdas said and after that i have got, the procedure is taking 1 to 10 seconds. Execution time is very machine to machine. And it's OK.

As you have pointed out, i am bound to agree with you, though i have got the result by using system time. Problem is that i am not getting the result using timer. I have tried like this........

1st one ----------------
Private Sub Form_Load()
Timer1.Interval = 100
Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
Label1.Caption = Val(Label1.Caption) + 1 '1 is equal to 0.1 second
End Sub

Private Sub Command1_Click()
Timer1.Enabled = True
'my all codes here
Timer1.Enabled = False
End Sub

here timer1 is not working or label1's caption(number) not increasing


2nd one --------------------
Dim cnt As Integer
Dim TM1 As Integer

Private Sub Form_Load()
cnt = 0
TM1 = 0
Timer1.Interval = 100
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
cnt = cnt + 1
End Sub

Private Sub Command1_Click()
cnt = 0
'my all codes here
TM1 = cnt
Label1.Caption = TM1
End Sub

Here also timer1 become pause at the time of running command1 procedure.

If any other different techniques or coding is require please guide. I want to use timer also.

This has already been marked as solved. Please open a new thread with your above question and the code, I have already converted the code which counts up to 0.001 of a second, which will give you a true indication.

Also post your loop in the new post, the timer actions will be contained there in. I will post a solution as soon as you have opened the new thread.:)

Also send me a PM that the new post is open, otherwise I might miss it whilst working.:)

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.