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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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;
// 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>
 
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.
--
Thanks,
Nick


Lloyd Dupont said:
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;
// 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>
 
Advised against a language construct is akin to an oxymoron.
don't you think?

--
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.
nickdu said:
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.
--
Thanks,
Nick


Lloyd Dupont said:
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;
// 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>

--
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.
nickdu said:
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?
 
Back
Top