473,473 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

ADezii
8,834 Recognized Expert Expert
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
0 24150

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: mark | last post by:
I understand that writing programs with option strict on is the best way to obtain stable applications. I have also found the applications to run much faster. Option strict on disallows late...
4
by: Justin Emlay | last post by:
I have two lists. These can be in either table form or array. That is, my data is in a dataset which I can move to an array if need be. ListA is a master list and it contains all items. ListB...
0
by: mark | last post by:
I have found that deployed "RELEASE" versions of applications employing matrix multiplications perform much more efficiently (1.5 to 2 X) when the algorithm accesses the data in inner loop of the...
5
by: Hugh | last post by:
Hi there, How to dim a array of arrays and index it? Let's say, array of 10 elements with each element is a (8, 12) 2-D array. Thanks in advance for your help. Hugh
6
by: MC | last post by:
Good Morning Anyone know any good lessons online about Arrays in vb.net. I am a hobbiest and stuggling to understand Arrays. Let me explain briefly what I want to do. I am writing a Poker...
3
by: new q. | last post by:
I've tried testing this to see if there is any difference but couldn't find any... is there any difference in doing a For - Next loop with/without a value after the 'Next' at the end? e.g....
2
by: Karl Groves | last post by:
I have a rather long form (122 SETS of questions - don't worry, it is just me using it, lol) I have the questions set up as "Survey ID, Question ID, Answer, Impact, Comments", which I have set...
5
by: Hermann.Richter | last post by:
These array functions: 'each', 'current', 'next', 'end' They return a reference or a value. let's say I want to modify the last value of an array without iterating through all of them. I...
22
by: J. Frank Parnell | last post by:
Hello, So, I was wondering how to do this: foreach($foo as $k=>$v AND $bar as $k2=>$v2){ echo '<TR><TD>$k</TD><TD>$v</TD><TD>$k2</TD><TD>$v2</TD></TR>; } Thanks,
3
by: smileyc | last post by:
I have 5 arrays, named array1 array2 array3 array4 array5. They are integer arrays each with 5 elements in them. I want to access all the elements in all the arrays using a for next loop,the pseudo...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.