Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Why is the ArrayList IEnumerator so much slower than it's IList in

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nickdu - 10 Aug 2005 15:04 GMT
From the tests that I have done it appears the ArrayList IEnumerator is about
three times slower than using it's IList indexer.  I would think they should
be roughly the same.  Is there a reason they're not?
Signature

Thanks,
Nick

Lloyd Dupont - 10 Aug 2005 15:26 GMT
I suppose you mesure with foreach?

foreach(object o in list)
{
   // do something?
}

well it expands to this code:
for(    IEnumerator e = list.GetEnumerator();     e.MoveNext(); )
{
   object o = e.Current;
   // do something
}
where
e.MoveNext() ~= return internalIndex ++ < list.Count;
e.Current ~= list[internalIndex];

which do a few more methods call compare to this one:
for(int i=0; i<Count; i++)
{
   object o = list[i];
   // do something
}

However I think the "performance" difference the 2 becomes more and more
marginal with the complexity of the code inside the loop.
So, unless you do a simple computation, I wouldn't worry.

If you do a simple computation and you have an ArrayList of value type, say
int you add boxing/unboxing operation "cost" (very marginal but you seems to
care about the 0.001% of the optimizable code).
you will be better using
int[] or, in 2.0 List<int>

Signature

If you're in a war, instead of throwing a hand grenade at the enemy, throw
one of those small pumpkins. Maybe it'll make everyone think how stupid war
is, and while they are thinking, you can throw a real grenade at them.
Jack Handey.

> From the tests that I have done it appears the ArrayList IEnumerator is
> about
> three times slower than using it's IList indexer.  I would think they
> should
> be roughly the same.  Is there a reason they're not?
nickdu - 10 Aug 2005 21:42 GMT
I agree that as the complexity of the contents of the loop increases the
overhead of the different approaches will be negligible.  However, I already
know of two people who suggest against using foreach and I assume it's
because of the performance issue I stated below.
Signature

Thanks,
Nick

> I suppose you mesure with foreach?
>
[quoted text clipped - 35 lines]
> > should
> > be roughly the same.  Is there a reason they're not?
Lloyd Dupont - 11 Aug 2005 10:08 GMT
Advised against a language construct is akin to an oxymoron.
don't you think?

Signature

If you're in a war, instead of throwing a hand grenade at the enemy, throw
one of those small pumpkins. Maybe it'll make everyone think how stupid war
is, and while they are thinking, you can throw a real grenade at them.
Jack Handey.

>I agree that as the complexity of the contents of the loop increases the
> overhead of the different approaches will be negligible.  However, I
[quoted text clipped - 43 lines]
>> > should
>> > be roughly the same.  Is there a reason they're not?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.