Click to See Complete Forum and Search --> : Time needed for execution


shashidharkr
October 1st, 2001, 01:18 AM
Hi,

How to find out the time taken for the execution of a piece of code. For example, consider the following sample code:

for i = 0 to 50000
Debug.print i
next



How to findout the execution time?

Thanks in advance

Cimperiali
October 1st, 2001, 02:17 AM
'You should use gettickcount Api before and avfter execution of code and than
'transform the difference in time.
'Beware: your machine will give different results each time, as your cpu will
'have different task /usage each time due to other kind of job doing in the
'meanwhile...

option Explicit
'In general section
private Declare Function GetTickCount Lib "kernel32" () as Long

private Sub Command1_Click()
Dim retStart as Long, retEnd as Long
Dim lngI as Long
Dim totMillisecod as Long
retStart = GetTickCount
for lngI = 0 to 50000
Debug.print lngI
next
retEnd = GetTickCount
'number of millisecond elapsed:
totMillisecod = retEnd - retStart
MsgBox "millisecond elapsed=" & totMillisecod & _
" Secod elapsed=" & totMillisecod / 1000

End Sub





Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater

shashidharkr
October 1st, 2001, 04:14 AM
Thanks Cimperiali for the response.

Now, I have got a different question, though not directly related with my previous question. When I execute the sample code mentioned in my question with a for loop of 50,000 cycles, it gives a runtime error 'Overflow'. This error generally comes with looping commands involving large cycles. But, we may have to execute large cycles in our projects. How to overcome this error?

Thanks in advance.

Cimperiali
October 1st, 2001, 05:08 AM
I saw you wrote:
for i = 0 to ...
i is usally dimensioned as Integer tYpe (=max value = 32.000).
You need a Long type.
So, use a :
dim lngI as long
for lngI = 0 to 50.000
...


Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater

shashidharkr
October 1st, 2001, 05:23 AM
You are right. i should have been long type indeed!!!
Many Thanks.

Cimperiali
October 1st, 2001, 05:39 AM
Have a nice day and happy coding.

Cesare Imperiali

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater

kirkslota
February 27th, 2002, 07:12 PM
One thing to watch out for here is if the tick counter wraps back to zero. Here's a little excerpt out of MSDN:

-------------------------------------------------
The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days.

The following example demonstrates how to handle timer wrap around.


DWORD dwStart = GetTickCount();

// Stop if this has taken too long
if( GetTickCount() - dwStart >= TIMELIMIT )
Cancel();
-------------------------------------------------