By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,680 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,680 IT Pros & Developers. It's quick & easy.

ArrayList: For Each vs Enumerator ... question

P: n/a
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
Share this Question
Share on Google+
18 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
Hi Rick,

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

Regards,
Fergus
Nov 20 '05 #7

P: n/a
:) 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

P: n/a
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

P: n/a
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

P: n/a
Hi Jay, Rick,

|| improper yoking of irrespective and regardless

Or is that 'improper yokeling' ?

Regards,
Fergus
Nov 20 '05 #11

P: n/a
:) 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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.