How about using LINQ or Yield Return in for loops?

I am currently working on a 2D-XNA game. It needs to be optimized because multi-user mode does not work well. In the code, we most often used foreach loops and did not use LINQ or returned return statements anywhere. I understand that we can win a certain performance here. Since it is faster for loops, I was thinking of replacing all foreach loops.

But, however, I cannot benefit from the return return statement in a for loop, can I?
Also, LINQ will still be useful when iterating using a for?


For example, I have a list of 1000 + shapes (squares, triangles, circles ...), and I want to list all the squares (75%) at a specific position. What is the best way to do this?
What should i use? Arrays, lists, for loops, foreach loops, return return and / or LINQ?

+4
source share
3 answers

Do any grouping or sorting that you need to do as the elements are not added in the way they are extracted. I say this because you (I suppose) add each element only once, but as you say, you extract them several times.

+4
source

Since the question of performance is the only correct answer, this is a measurement.

In general, directly using for will most likely be the fastest approach, as the rest will add more code for each iteration. Try and measure yourself - see if it matters in your case and which version of the code you like to read the most.

+4
source

Definitely use a For loop. If not to improve performance, which will be negligible, you will not create an enumerator object. Therefore, you do not put unnecessary pressure on the GC.

0
source

All Articles