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?