Which is faster for arrays - foreach or for ( i... ) ?

  • Thread starter Thread starter DoB
  • Start date Start date
D

DoB

Hi,

Given the following declaration:

object[] a = new object[SOME_BIG_NUMBER];

which code is faster?

1.
foreach ( object o in a )
DoSomethingWith(o);

2.
int iSize = a.Length;
int i;
foreach ( i = 0; i < iSize; i++ )
DoSomethingWith(a);

And why?

Regards,
DoB
 
foreach ( i = 0; i < iSize; i++ )

Sorry, there was a typo here, of course ;-). should be "for".

DoB
 
DoB said:
Given the following declaration:

object[] a = new object[SOME_BIG_NUMBER];

which code is faster?

1.
foreach ( object o in a )
DoSomethingWith(o);

2.
int iSize = a.Length;
int i;
foreach ( i = 0; i < iSize; i++ )
DoSomethingWith(a);

And why?


In general, "for i" used with an array is faster than "foreach", the
reason being that "for i" only needs to increment and compare an integer and
do an indexing operation for every iteration, which are fast operations,
while "foreach" needs to call into IEnumerator to move to the next element
and then retrieve the current one. These are two method calls, in addition
to whatever is inside the methods, so it has a bigger overhead.
 
Given the following declaration:

object[] a = new object[SOME_BIG_NUMBER];

which code is faster?

I wouldn't concentrate on which code is faster until I've proved it
matters - at which point I'd have numbers to compare if I made
changes.

I'd concentrate on which is more readable, and simpler - in which case
"foreach" wins in most situations.

Don't micro-optimise until you know you've got a problem - and if you
know you've got a problem, that should include hard figures, which
will make it easy to test changes.

Jon
 
Don't micro-optimise until you know you've got a problem - and if you
know you've got a problem, that should include hard figures, which
will make it easy to test changes.

I actually came across a situation in which I wanted to change "foreach" to
"for" for some reasons (not performance-related) and before making a change
I wanted to know what was an impact on performance.

DoB
 
I actually came across a situation in which I wanted to change "foreach" to
"for" for some reasons (not performance-related) and before making a change
I wanted to know what was an impact on performance.

Miniscule either way, unless you're really, really not doing anything
in the loop. If you have performance concerns, measuring them is
crucial.

I wouldn't try to micro-optimise the for loop, btw. Just do:

for (int i=0; i < myArray.Length; i++)
{
}

instead of using the temporary variable. I seem to recall that this is
actually faster in some situations, but it's certainly more readable.

Jon
 
I wouldn't try to micro-optimise the for loop, btw. Just do:
for (int i=0; i < myArray.Length; i++)
{
}

instead of using the temporary variable. I seem to recall that this is
actually faster in some situations, but it's certainly more readable.


I've made some tests and... yes... you are absolutely right - this is a bit
faster.
Why? Optimisation?

DoB.
 
DoB said:
I've made some tests and... yes... you are absolutely right - this is a bit
faster.
Why? Optimisation?

The JIT compiler recognises the pattern, and can remove the array
bounds check within the loop, as I understand it.
 
Just my 2 cents.. I find the Array.ForEach<> most readable, but maybe not
the most efficient:

object[] arr = new object[SOME_BIG_NUMBER];
Array.ForEach<object>(arr, new Action<object>(DoSomething));

Jon Skeet said:
Given the following declaration:

object[] a = new object[SOME_BIG_NUMBER];

which code is faster?

I wouldn't concentrate on which code is faster until I've proved it
matters - at which point I'd have numbers to compare if I made
changes.

I'd concentrate on which is more readable, and simpler - in which case
"foreach" wins in most situations.

Don't micro-optimise until you know you've got a problem - and if you
know you've got a problem, that should include hard figures, which
will make it easy to test changes.

Jon
 
Alberto said:
DoB said:
Given the following declaration:

object[] a = new object[SOME_BIG_NUMBER];

which code is faster?

1.
foreach ( object o in a )
DoSomethingWith(o);

2.
int iSize = a.Length;
int i;
foreach ( i = 0; i < iSize; i++ )
DoSomethingWith(a);

And why?


In general, "for i" used with an array is faster than "foreach", the
reason being that "for i" only needs to increment and compare an
integer and do an indexing operation for every iteration, which are
fast operations, while "foreach" needs to call into IEnumerator to
move to the next element and then retrieve the current one. These are
two method calls, in addition to whatever is inside the methods, so
it has a bigger overhead.


arrays are a special case for the foreach keyword, the compiler
automatically avoids using IEnumerator
 
Dejan Stanic said:
Definitely not efficient.

Well, to the extent that there's a delegate call each time. As ever,
it's unlikely to be significant in most code.
 
I've made some tests and... yes... you are absolutely right - this is a
The JIT compiler recognises the pattern, and can remove the array
bounds check within the loop, as I understand it.

What is interesting, I tested in debug mode. Looks like it is optimised,
too.

DoB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top