How to find out which nested for loop is better?

前端 未结 5 1078
无人及你
无人及你 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:36

    Have you actually tried it?

    I would assume theoretically Case II would be marginally faster since there would only be half of the stack-based variable creation / destruction in the outer loop than there would be for Case I, but even better (and clearer) would be:

    for(int k = 0; k < 50; k++)
    {
         count++;
    }
    

    To be precise, your examples are so abstracted that the answer is probably of little use. It very much depends on the context.

提交回复
热议问题