C# - 对于循环内部 [英] C# - For-loop internals

查看:111
本文介绍了C# - 对于循环内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个快速,简单的问题,从我关于for循环。

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

状况
我目前正在写一些高当我突然想知道for循环实际上是如何表现性能的代码。我知道我以前碰到这个迷迷糊糊的,但不能为我的生活再次找到该信息:/

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
}


是something.awesome存储为一个内部变量或者是循环不断地检索something.awesome做逻辑检查?为什么我问的是当然的,因为我经历了很多的东西,索引需要循环,我真的不希望每个通额外的函数调用的开销。

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.

但是,如果something.awesome只叫一次,然后我在我快乐的岩石下回去! :)

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

推荐答案

您可以使用一个简单的示例程序检查行为:

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.



此行为的详细信息将在C#4.0语言规范,8.3.3节(你描述发现里面规范的 C:\Program Files\Microsoft的Visual Studio 10.0\VC#\Specifications\1033 的):

有关语句时,作为
执行如下:

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.

如果for条件不存在,或者如果计算产生真实,
控制被转移到嵌入式
语句。 当(如果)控制到达
嵌入式
语句的结束点(可能是
A的执行continue语句)的对迭代器,如果有任何表情
都是
在顺序评估,然后进行
另一迭代,
开始与
为条件在上述步骤的评价

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.

如果for条件是存在和计算产生假,控制
被转移到
for语句的结束点。

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

这篇关于C# - 对于循环内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆