PDA

View Full Version : Solved: Timing a For Loop



Saladsamurai
11-16-2009, 11:02 AM
I know I have seen this done before, but I can't seem to figure out how.

I want to see how long it takes to run a certain For loop by doing something to the effect of:




Sub StopWatch()


Dim StartTime As Variant
Dim TimeElapsed As Variant
Dim i As Long
i = 0
StartTime = Time()
Do While i < 100000000
i = i + 1
Loop

TimeElapsed = Time() - StartTime

MsgBox TimeElapsed

End Sub



Any ideas? This returns a number that is clearly way too small (on my machine) to be the time elapsed. This Loop took at least 3 seconds to complete but the Value MsgBoxed was less than 1.

The Time() function returns a Variant.

nst1107
11-16-2009, 11:05 AM
Try this:Sub StopWatch()


Dim StartTime As Single
Dim TimeElapsed As Single
Dim i As Long
i = 0
StartTime = Timer
Do While i < 100000000
i = i + 1
Loop

TimeElapsed = Timer - StartTime

MsgBox TimeElapsed

End Sub

Saladsamurai
11-16-2009, 11:14 AM
oooo that's the stuff! Thanks!