|
-
October 1st, 2001, 01:18 AM
#1
Time needed for execution
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
-
October 1st, 2001, 02:17 AM
#2
Re: Time needed for execution
'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
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
October 1st, 2001, 04:14 AM
#3
Re: Time needed for execution
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.
-
October 1st, 2001, 05:08 AM
#4
Re: Time needed for execution
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
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
October 1st, 2001, 05:23 AM
#5
Re: Time needed for execution
You are right. i should have been long type indeed!!!
Many Thanks.
-
October 1st, 2001, 05:39 AM
#6
Re: Happy it helped
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
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
February 27th, 2002, 08:12 PM
#7
Re: Time needed for execution
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();
-------------------------------------------------
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|