By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,870 Members | 1,224 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

In Arrays, For Each...Next vs For...Next

ADezii
Expert 5K+
P: 8,669
If you want to visit each item in an Array, you have two alternatives:
  1. Use a For Each..Next loop, using a Variant to retrieve each value in turn.
  2. Use a For...Next loop, looping from the Lower Bound to the Upper Bound of the Array.
For Each...Next seems simpler because you need not worry about retrieving the Lower and Upper Bounds- the loop takes care of that for you.
Expand|Select|Wrap|Line Numbers
  1. For Each varValue In alngValues
  2.     j = varValue
  3. Next varValue
Using For...Next requires a bit more effort on your part because you must write the code that finds the Lower and Upper Bounds.
Expand|Select|Wrap|Line Numbers
  1. For lngCount = LBound(alngValues) To UBound(alngValues)
  2.     j = alngValues(lngCount)
  3. Next lngCount
You might think that the For Each...Next would be faster, because it requires less code but that isn't so. The For...Next loop will give you better performance if you're working with Arrays. In tests, the faster version took about 70% as long as the slower version. Here is the actual code that will prove my point. Take special note of the timeGetTime() API Function to act as the Stopwatch:
Expand|Select|Wrap|Line Numbers
  1. 'First, the API Declaration
  2. Public Declare Function timeGetTime Lib "winmm.dll" () As Long
Expand|Select|Wrap|Line Numbers
  1. Public Function fForEach_Next(lngRepeats As Long) As Long
  2. 'Slower version of code using For Each...Next to process Array Elements
  3.  
  4. Dim alngValues(1 To 1000) As Long
  5. Dim varValue As Variant, i As Long, j As Long
  6. Dim lngStartTime As Long
  7.  
  8. For i = 1 To 1000
  9.   alngValues(i) = i
  10. Next i
  11.  
  12. lngStartTime = timeGetTime()
  13. For i = 1 To lngRepeats
  14.   For Each varValue In alngValues
  15.     j = varValue
  16.   Next varValue
  17. Next i
  18. fForEach_Next = (timeGetTime() - lngStartTime)
  19. End Function
Expand|Select|Wrap|Line Numbers
  1. Public Function fFor_Next(lngRepeats As Long) As Long
  2. 'Faster version of code using For...Next to process Array Elements
  3.  
  4. Dim alngValues(1 To 1000) As Long
  5. Dim lngCount As Long, i As Long, j As Long, T As Long
  6. Dim lngStartTime As Long
  7.  
  8. For i = 1 To 1000
  9.   alngValues(i) = i
  10. Next i
  11.  
  12. lngStartTime = timeGetTime()
  13. For i = 1 To lngRepeats
  14.   For lngCount = LBound(alngValues) To UBound(alngValues)
  15.     j = alngValues(lngCount)
  16.   Next lngCount
  17. Next i
  18. fFor_Next = (timeGetTime() - lngStartTime)
  19. End Function
WARNING! - Although you can use either of these techniques to read items from an Array, you can only use the For...Next loop to write into Array elements. The For Each...Next loop retrieves a copy of the data in the Array, not the actual Array element itself. Although you won't receive an Error if you use For Each...Next to write into an Array, the data will not actually go into the Array.
Oct 7 '07 #1
Share this Article
Share on Google+