Y
Y
Yuri2015-10-08 15:55:30
Unity
Yuri, 2015-10-08 15:55:30

For or foreach loops in Unity3d?

Hello, can you advise which is better private use of for or foreach loop, which has the least impact on performance in a Unity3d project?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2015-10-08
@Gilga

We use foreach at home, they are more convenient, but we don’t observe any brakes. If the profiler says that it slows down, replace it with for. Do not bother with optimization ahead of time, write as convenient. Brakes will appear, look at the profiler, draw conclusions.

A
Alexander, 2015-10-08
@xa3apg

in fact, for will be faster. mono will recompile foreach and while into IEnumerable + switching methods

C
Curly Brace, 2015-10-08
@stasuss

mono seems to compile them in while yes. but not the fact that still. for example, if you use a compiler from the studio, then there is no such problem and foreach fly for generics of the List<> type. you need to research ..
here is sort of relevant research:
www.somasim.com/blog/2015/04/csharp-memory-and-per...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question