Efficiency of foreach vs for loop

J

Jim H

When dealing with reference types, is there an efficiency advantage in using
a for loop instead of a foreach? I though I read somewhere that when using
value types a for loop was more efficient because of the boxing involved
when using foreach.

Thanks,
Jim
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,



| When dealing with reference types, is there an efficiency advantage in
using
| a for loop instead of a foreach? I though I read somewhere that when
using
| value types a for loop was more efficient because of the boxing involved
| when using foreach.

This has been discussed here ad-nauseam, look in the archives for those
thread.

The conclusion is that you should your time focused in more important
matters :)

I do not see how the boxing can be avoided with either solution though.
 
J

Jon Skeet [C# MVP]

Jim H said:
When dealing with reference types, is there an efficiency advantage in using
a for loop instead of a foreach? I though I read somewhere that when using
value types a for loop was more efficient because of the boxing involved
when using foreach.

It depends on *exactly* which collection you're looking through.
However, usually the difference is so small, you should use whichever
you find more readable. (IMO that's usually foreach.)
 
J

Jim H

Thanks to both of you (John and Ignacio).

I'll just use whatever looks easier to read for others in each situation.

jim
 
B

Bruce Wood

When dealing with reference types, is there an efficiency advantage in using
a for loop instead of a foreach? I though I read somewhere that when using
value types a for loop was more efficient because of the boxing involved
when using foreach.

There are situations in which "for" is definitely worse, as well. For
example, if you have a property that builds and returns an aggregate
structure such as an array:

public MyThing[] MyThings
{
get { return
(MyThing[])this._internalMyThings.ToArray(typeof(MyThing)); }
}

(Yes, I know that this is bad practice, and that FxCop specifically
warns against it, but say you had a property like this....)

Then this

for (int x = 0; x < myObj.MyThings.Length; x++)
{
MyThing thing = myObj.MyThings;
}

gives absolutlely horrible performance, while this:

foreach (MyThing thing in myObj.MyThings)
{
}

gives good performance.

Again, it all depends on what you're doing, and in the end it's
usually bad designs that kill performance, not using one style of loop
versus another.
 
J

Jeffrey Tan[MSFT]

Hi Jim,

Yes, I also agree with others that we should pay more attention to other
more performance concern places. However, if your application manipulates
string very often, the "for" may improve some performance, please search
"Use For Loops for String Iteration" section in the link below:
"Performance Tips and Tricks in .NET Applications"
http://msdn2.microsoft.com/en-us/library/ms973839.aspx

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Top