Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

Which has better performance for or foreach ?

Thanks in Advance.
Posted

foreach wil be creating a new object and there is a cost associated with this.
Some enumerators will check if the solution is changed and there are also a cost for it.
This said, don't fall to quick into this trap. I had a student once who decided to change all the foreach( into a for(int x=1;....
It made the code harder to read and the benefit was not noticeable.

The secret of performance tweaking is to first identify which bits are slow.
Then and only then you can act on these bits and leave the rest undisturbed.
 
Share this answer
 
 
Share this answer
 
Comments
[no name] 8-Nov-11 23:12pm    
Thanks for the valuable links.
You're a programmer. Write an app that uses each of the methods in question and see for yourself. Alternatively, you can just google it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900