PC Review


Reply
Thread Tools Rate Thread

ArrayList: For Each vs Enumerator ... question

 
 
Rob Panosh
Guest
Posts: n/a
 
      7th Oct 2003
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






 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      7th Oct 2003
"Rob Panosh" <rob_!!!NO!!!SPAM!!!_(E-Mail Removed)>
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

 
Reply With Quote
 
Rafael Pivato
Guest
Posts: n/a
 
      7th Oct 2003
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" <rob_!!!NO!!!SPAM!!!_(E-Mail Removed)> escreveu na
mensagem news:(E-Mail Removed)...
> 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
>
>
>
>
>
>



 
Reply With Quote
 
Rob Panosh
Guest
Posts: n/a
 
      7th Oct 2003
Thanks ...

"Armin Zingler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Rob Panosh" <rob_!!!NO!!!SPAM!!!_(E-Mail Removed)>
> 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
>



 
Reply With Quote
 
Rob Panosh
Guest
Posts: n/a
 
      7th Oct 2003
Thanks ...

"Rafael Pivato" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <rob_!!!NO!!!SPAM!!!_(E-Mail Removed)> escreveu na
> mensagem news:(E-Mail Removed)...
> > 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
> >
> >
> >
> >
> >
> >

>
>



 
Reply With Quote
 
Fergus Cooney
Guest
Posts: n/a
 
      7th Oct 2003
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



 
Reply With Quote
 
Rafael Pivato
Guest
Posts: n/a
 
      7th Oct 2003
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)

---
Rafael Pivato


"Fergus Cooney" <filter-(E-Mail Removed)> escreveu na mensagem
news:(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
Fergus Cooney
Guest
Posts: n/a
 
      7th Oct 2003
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


 
Reply With Quote
 
Rob Panosh
Guest
Posts: n/a
 
      7th Oct 2003
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" <filter-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
Fredrik Melin
Guest
Posts: n/a
 
      7th Oct 2003
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" <rob_!!!NO!!!SPAM!!!_(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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" <filter-(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Enumerator question csharpula csharp Microsoft C# .NET 7 16th Dec 2006 04:29 PM
ArrayList as Enumerator in JScript Jayme Pechan Microsoft C# .NET 2 21st Aug 2006 10:15 PM
database enumerator like datasource enumerator =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= Microsoft ADO .NET 3 10th Mar 2006 05:48 AM
Enumerator to String to Enumerator =?Utf-8?B?QmFyZ3Vhc3Q=?= Microsoft C# .NET 3 24th Dec 2004 10:13 PM
Arraylist simple question (newbie) OT (for a CF project, but not necessarily a CF question?) Keith R Microsoft Dot NET Compact Framework 1 2nd Dec 2003 10:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:41 PM.