473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ArrayList: For Each vs Enumerator ... question

Hello,

When traversing an ArrayList which is faster?

For Each oItem as Something in me.ArrayList
.....
.....
Next

OR

Dim oItem as Something
Dim Enumerator as IEnumerator = me.ArrayList.GetEnumerator()

While Enumerator.MoveNext()
...
...
End While

Thanks,
Rob Panosh


Nov 20 '05 #1
18 6608
I think you will feel no difference other than a beautiful code with the
first option.

The For Each statement also uses the IEnumerator interface, but it
simplifies the use for you.

-----------

Rafael Pivato

"Rob Panosh" <ro************************@asdsoftadfdware.com> escreveu na
mensagem news:ei**************@TK2MSFTNGP11.phx.gbl...
Hello,

When traversing an ArrayList which is faster?

For Each oItem as Something in me.ArrayList
....
....
Next

OR

Dim oItem as Something
Dim Enumerator as IEnumerator = me.ArrayList.GetEnumerator()

While Enumerator.MoveNext()
...
...
End While

Thanks,
Rob Panosh


Nov 20 '05 #2
"Rob Panosh" <ro************************@asdsoftadfdware.com>
schrieb
Hello,

When traversing an ArrayList which is faster?

For Each oItem as Something in me.ArrayList
....
....
Next

OR

Dim oItem as Something
Dim Enumerator as IEnumerator = me.ArrayList.GetEnumerator()

While Enumerator.MoveNext()
...
...
End While


Both versions are equal. For Each also uses the IEnumerator interface.
--
Armin

Nov 20 '05 #3
Thanks ...

"Rafael Pivato" <rp*****@cpovo.net> wrote in message
news:eX**************@tk2msftngp13.phx.gbl...
I think you will feel no difference other than a beautiful code with the
first option.

The For Each statement also uses the IEnumerator interface, but it
simplifies the use for you.

-----------

Rafael Pivato

"Rob Panosh" <ro************************@asdsoftadfdware.com> escreveu na
mensagem news:ei**************@TK2MSFTNGP11.phx.gbl...
Hello,

When traversing an ArrayList which is faster?

For Each oItem as Something in me.ArrayList
....
....
Next

OR

Dim oItem as Something
Dim Enumerator as IEnumerator = me.ArrayList.GetEnumerator()

While Enumerator.MoveNext()
...
...
End While

Thanks,
Rob Panosh



Nov 20 '05 #4
Thanks ...

"Armin Zingler" <az*******@freenet.de> wrote in message
news:u9**************@TK2MSFTNGP12.phx.gbl...
"Rob Panosh" <ro************************@asdsoftadfdware.com>
schrieb
Hello,

When traversing an ArrayList which is faster?

For Each oItem as Something in me.ArrayList
....
....
Next

OR

Dim oItem as Something
Dim Enumerator as IEnumerator = me.ArrayList.GetEnumerator()

While Enumerator.MoveNext()
...
...
End While


Both versions are equal. For Each also uses the IEnumerator interface.
--
Armin

Nov 20 '05 #5
Yes you are right, I mixed up, I was when I was working with collection the
difference between FOR EACH and FOR NEXT is so big.

/poke my self.

- Fredrik

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:Ob**************@TK2MSFTNGP09.phx.gbl...
Fredrik,
You forget one important factor also, FOR EACH takes alot of memory
(depending on your array size) FOR EACH does not use a lot of memory! Normally an Enumerator needs to

store 2 pieces of information, the current position & the 'list' it is
enumerating.

Irregardless of the size of the arraylist, the size of a reference to that
ArrayList & the size of the current index into the array list is going to
remain constant.

When you call ArrayList.GetEnumerator normally get a
System.Collections.ArrayList.ArrayListEnumeratorSi mple returned.

If you use ISDASM.EXE on mscorlib.dll (the assembly where ArrayList is) you will find that the above class contains 4 fields.
- currentElement
- index
- list
- version

Even ArrayListEnumerator only has 2 more fields (startIndex & endIndex).

Which definitely is not alot of memory!

Speed wise For Next may well be faster, however I normally use For Each to
be consistent across all collection types. Remember some collections do not support indexing by integers. Also indexing by an integer may actually be an expensive operation based on the type of collection (a true Linked List for example). What may be true for Array & ArrayList will not necessarily be
true for all collections.

Hope this helps
Jay

"Fredrik Melin" <me*@n.o.spam.dacsa.net> wrote in message
news:se********************@giganews.com...
You forget one important factor also, FOR EACH takes alot of memory
(depending on your array size)
So, as stated, FOR NEXT is in most cases alot better.

Regards
Fredrik Melin

"Rob Panosh" <ro************************@asdsoftadfdware.com> wrote in
message news:OQ*************@TK2MSFTNGP11.phx.gbl...
Fergus,

Thanks for you post.

So if I have routines traversing lots of items in collections then the

code
FOR NEXT (below) would be better than using FOR EACH? So I am
guessing
the
rule of thumb is to use FOR NEXT over FOR EACH for heavliy used routines.
Thanks,
Rob Panosh

Dim oItem AS Something
For i = 0 to al.count-1
oItem = al.Item(i)
' Do some processing
...
...
...
next

For Each oItem As Something In al
'Do some processing
...
...
...
Next

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:O7**************@tk2msftngp13.phx.gbl...
> Hi Rob,
>
> For Each uses an enumerator behind the scenes so, as expected,
on the
> tests that I did, they came out even (to the 3rd dec place). The
difference is
> almost certainly due to timng inaccuracies.
>
> On the other hand - using For I = 0 To al.Count - 1 was three

times > faster!
>
> Regards,
> Fergus
>
>
>



Nov 20 '05 #6
Hi Rick,

What's the peeve? Just that word or words in general?

Regards,
Fergus
Nov 20 '05 #7
:) That one in particular. A few others, like "unthaw" and when people say "Hot water heater" or
"I could care less"

I dont know, its just funny when people say things because they have hear them so many times, but
when thought about, what they are saying is not what they mean.

Rick

"Fergus Cooney" <fi******@tesco.net> wrote in message news:Ov*************@TK2MSFTNGP12.phx.gbl...
Hi Rick,

What's the peeve? Just that word or words in general?

Regards,
Fergus

Nov 20 '05 #8
Rick,
Irregardless is not a word. Well it is, but it ain't! ;-)

Based on the following, which I found on a quick Google search, its a word
that falls into what I would call the "Poor Grammar" category, but a word
never the less.

http://dictionary.reference.com/search?q=irregardless

<blockquote>
Usage Note: Irregardless is a word that many mistakenly believe to be
correct usage in formal style, when in fact it is used chiefly in
nonstandard speech or casual writing. Coined in the United States in the
early 20th century, it has met with a blizzard of condemnation for being an
improper yoking of irrespective and regardless and for the logical absurdity
of combining the negative ir- prefix and -less suffix in a single term.
Although one might reasonably argue that it is no different from words with
redundant affixes like debone and unravel, it has been considered a blunder
for decades and will probably continue to be so.
</blockquote>

Note the spell checker did not flag it, so I used it.

Thanks for letting me know!

Jay

"Rick Mogstad" <ri**@NOSPAM.computetosuit.com> wrote in message
news:Oc**************@TK2MSFTNGP12.phx.gbl...
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message news:Ob**************@TK2MSFTNGP09.phx.gbl...

Irregardless of the size of the arraylist, the size of a reference to that ArrayList & the size of the current index into the array list is going to remain constant.

Irregardless is not a word.

Sorry, pet peeve I guess.


Nov 20 '05 #9
Fine then,its a word, but its a rediculous way to say what you werent intending to say anyhow :)
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:O6*************@tk2msftngp13.phx.gbl...
Rick,
Irregardless is not a word.

Well it is, but it ain't! ;-)

Based on the following, which I found on a quick Google search, its a word
that falls into what I would call the "Poor Grammar" category, but a word
never the less.

http://dictionary.reference.com/search?q=irregardless

<blockquote>
Usage Note: Irregardless is a word that many mistakenly believe to be
correct usage in formal style, when in fact it is used chiefly in
nonstandard speech or casual writing. Coined in the United States in the
early 20th century, it has met with a blizzard of condemnation for being an
improper yoking of irrespective and regardless and for the logical absurdity
of combining the negative ir- prefix and -less suffix in a single term.
Although one might reasonably argue that it is no different from words with
redundant affixes like debone and unravel, it has been considered a blunder
for decades and will probably continue to be so.
</blockquote>

Note the spell checker did not flag it, so I used it.

Thanks for letting me know!

Jay

"Rick Mogstad" <ri**@NOSPAM.computetosuit.com> wrote in message
news:Oc**************@TK2MSFTNGP12.phx.gbl...

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in

message
news:Ob**************@TK2MSFTNGP09.phx.gbl...

Irregardless of the size of the arraylist, the size of a reference to that ArrayList & the size of the current index into the array list is going to remain constant.

Irregardless is not a word.

Sorry, pet peeve I guess.


Nov 20 '05 #10
Hi Jay, Rick,

|| improper yoking of irrespective and regardless

Or is that 'improper yokeling' ?

Regards,
Fergus
Nov 20 '05 #11
:) OK, i looked it up this time. "yokeling" is definitely not a word!
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:eo**************@TK2MSFTNGP12.phx.gbl...
Hi Jay, Rick,

|| improper yoking of irrespective and regardless

Or is that 'improper yokeling' ?

Regards,
Fergus

Nov 20 '05 #12
Hi Rick,

Ah, yes - you mean the negated negatives and their brethren.

"I could care less" makes me grimace too. How much less then? or So you
<do> care!??

Well, in all the postings that I've made, I hope I haven't done nothing to
make you wince. ;-)

Regards,
Fergus
Nov 20 '05 #13
Hi Rick,

Yes it is! It's when you yokelify something - you know, make like a yokel
with it!!

After much yokelification, the situation degeneralised something chronic.

;-))

Regards,
Fergus
Nov 20 '05 #14
Well, its a funny non-word anyhow. :)
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:O4**************@TK2MSFTNGP09.phx.gbl...
Hi Rick,

Yes it is! It's when you yokelify something - you know, make like a yokel
with it!!

After much yokelification, the situation degeneralised something chronic.

;-))

Regards,
Fergus

Nov 20 '05 #15
Fergus,
As long as you don't start any "improper yodelling"

Jay

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:eo**************@TK2MSFTNGP12.phx.gbl...
Hi Jay, Rick,

|| improper yoking of irrespective and regardless

Or is that 'improper yokeling' ?

Regards,
Fergus

Nov 20 '05 #16
Hi Jay,

|| "improper yodelling"

Ha, ha - you can't hear what's happening at this PC. All I can say is that
it's a good job we're in a text-only environment - yodelloo-ho-ho,
yodelloo-hee-hay, lol. :-))

Regards,
Fergus
Nov 20 '05 #17
Rick,

How about:

For Each oItem Irregardless of Type In ArrayList
.....
.....
Next

Cheers,
Rob
"Rick Mogstad" <ri**@NOSPAM.computetosuit.com> wrote in message
news:Oc**************@TK2MSFTNGP12.phx.gbl...

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message news:Ob**************@TK2MSFTNGP09.phx.gbl...

Irregardless of the size of the arraylist, the size of a reference to that ArrayList & the size of the current index into the array list is going to remain constant.

Irregardless is not a word.

Sorry, pet peeve I guess.

Nov 20 '05 #18
Cor
Hi Fergus,
Nice thread again,
I am curious, how much was the difference in nanosecond, (not the actual
time)?
Cor
Nov 20 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

80
by: | last post by:
Is there a performance difference between this: \\\ Dim i As Integer For i = 0 to myObject.Controls.Count - 1 myObject.Controls(i) = ... Next /// and this:
0
by: Stephen | last post by:
I have been getting on well with help from this forum trying to create an array list and work with it. Everything is working fine apart from displaying my array list items into the labels in my...
9
by: Lloyd Dupont | last post by:
Does any of you have the slightest ideas of why you can't modify an array list while in a foreach of its element. I wrote my own collection and I'm trying to add the same behavior but I don't...
7
by: Steven | last post by:
I have 2 arraylists (name and value). Is there anyway that I can make that into one arraylist with 2 columns. First column as name and second column as value? -- Steven
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
3
by: Mark Jones | last post by:
I am quite new to ASP and .Net development and I am building a web-based multiple choice exam application. The web page displays the questions using a Repeater control and the answers are nested...
2
by: Jayme Pechan | last post by:
I migrated a formerly C++ COM component to C# and I have been able to duplicate all the old behavior except for one. I have an enumeration property that returns a list of com objects. Using the...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
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...
0
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,...
0
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...
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,...

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.