How to find out which nested for loop is better?

前端 未结 5 1014
无人及你
无人及你 2021-01-21 16:14

Somebody asked me a question: Which one is the fastest among the below two scenario:

Case 1: assume int count = 0;

for (int i = 0; i < 10; i++)
{
           


        
5条回答
  •  花落未央
    2021-01-21 16:47

    Actually, it is in general not a good idea to try to optimize yourself for such details, because mostly the compiler is much better at it (as long as the algorithm is not changed).

    The number of loops is equal.

    It might be that the second is a bit faster, because the initialization of the second loop only happens 5 times instead of 10, but I doubt that would really gain some noticable change.

    The best way is to use a profiler or even better: analyze the generated assembly code.

提交回复
热议问题