473,800 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ 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 24178

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

Similar topics

2
1733
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 binding, so if I have an array with class level scope and I am operating on that array at procedure level I fabricate a "dummy array" and copy the class level array into it. Then after performing the task I can copy the "dummy array" back into the...
4
1994
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 contains the same items but not necessarily all the items. I need to know which items are missing. I remember messing with a compare array function but now I can't seem to find it anywhere. Ideas? I would rather NOT loop through each item in...
0
982
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 multiplication in a row by row fashion. For example: For k = 1 to m : For i = 1 to n : For j = 1 to p C(i, j) = A(i, k)*B(k, j) + C(i, j) Next : Next : Next
5
1366
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
3673
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 Solutions Program purely for my oen fun and want to shuffle a deck of cards. now the easiest way, i think, is to make an Array that looks like this:
3
1252
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. --> For x in 1 to 10 'statements Next versus
2
1468
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 up so that each question is an array, like so: ques_1 ques_1 ques_1 ques_1
5
1650
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 would do: <?
22
38514
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
1731
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 code goes something like this: for i = 1 to 5 'this loop calls each array for j = 0 to 4 ' this loop calls each element if array (i,j) then ' code goes here end if next j next i
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10507
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10279
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10255
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9092
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5473
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.