ArrayList: For Each vs Enumerator ... question

  • Thread starter Thread starter Rob Panosh
  • Start date Start date
R

Rob Panosh

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
 
Rob Panosh said:
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.
 
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.
 
Thanks ...

Rafael Pivato said:
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.
 
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
 
Yep, nice post....

And the reason for it is simple:
you will naturally have fast For
statements using integers
(no method calls) and to get
the element you just have
to do one call for each iteration.

Using the enumerator you
do some abstracts, but you
pay for it, calling one method
just to verify if it is the end.

But IEnumerator still having
various advantages... (maybe
not performance like)
 
Hi Rafael,

For Each/IEnumerator does indeed advantages. Also the timing was done on a
loop which did very little (a single method call on the object and an
addition - so that it wouldn't all be optimised away). In practice the three
times faster of the For-I loop may well be trivial compared to the cost of the
work inside the loop.

Regards,
Fergus
 
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
 
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
 
Fredrik,



FOR EACH doesn't takes a lot of memory. ArrayList implementation/use of
IEnumerable (and IEnumerator) could do that. You can have an implementation
that supersedes allocation and speeds considerations.
 
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.ArrayListEnumeratorSimple 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
 
Jay B. Harlow said:
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.
 
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
 
:) 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
 
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
 
Fine then,its a word, but its a rediculous way to say what you werent intending to say anyhow :)
 
Hi Jay, Rick,

|| improper yoking of irrespective and regardless

Or is that 'improper yokeling' ?

Regards,
Fergus
 
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
 
Back
Top