Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

performance - C# - For-loop internals

a quick, simple question from me about for-loops.

Situation I'm currently writing some high-performance code when I suddenly was wondering how the for-loop actually behaves. I know I've stumbled across this before, but can't for the life of me find this info again :/

Still, my main concern was with the limiter. Say we have:

for(int i = 0; i < something.awesome; i++)
{
// Do cool stuff
}

Question Is something.awesome stored as an internal variable or is the loop constantly retrieving something.awesome to do the logic-check? Why I'm asking is of course because I need to loop through a lot of indexed stuff and I really don't want the extra function-call overhead for each pass.

However if something.awesome is only called once, then I'm going back under my happy rock! :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use a simple sample program to check the behaviour:

using System;

class Program
{
    static int GetUpperBound()
    {
        Console.WriteLine("GetUpperBound called.");
        return 5;
    }

    static void Main(string[] args)
    {
        for (int i = 0; i < GetUpperBound(); i++)
        {
            Console.WriteLine("Loop iteration {0}.", i);
        }
    }
}

The output is the following:

GetUpperBound called. 
Loop iteration 0. 
GetUpperBound called. 
Loop iteration 1. 
GetUpperBound called. 
Loop iteration 2. 
GetUpperBound called. 
Loop iteration 3. 
GetUpperBound called. 
Loop iteration 4. 
GetUpperBound called.

The details of this behaviour are described in the C# 4.0 Language Specification, section 8.3.3 (You will find the spec inside C:Program FilesMicrosoft Visual Studio 10.0VC#Specifications1033):

A for statement is executed as follows:

  • If a for-initializer is present, the variable initializers or statement expressions are executed in the order they are written. This step is only performed once.

  • If a for-condition is present, it is evaluated.

  • If the for-condition is not present or if the evaluation yields true, control is transferred to the embedded statement. When and if control reaches the end point of the embedded statement (possibly from execution of a continue statement), the expressions of the for-iterator, if any, are evaluated in sequence, and then another iteration is performed, starting with evaluation of the for-condition in the step above.

  • If the for-condition is present and the evaluation yields false, control is transferred to the end point of the for statement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...