引用类型变量回收 - 是在一个循环中创建的每个环一个新的参考变量,如果其中的声明? [英] Reference type variable recycling - is a new reference variable created every loop in a loop if declared therein?

查看:153
本文介绍了引用类型变量回收 - 是在一个循环中创建的每个环一个新的参考变量,如果其中的声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是:

MyObject myVariable;
for(int i = 0; i < objects.Length, i++){
  myVariable = objects[i];
  // do stuff...
}

更有效的话:

for(int i = 0; i < objects.Length, i++){
  MyObject myVariable = objects[i];
  // do stuff...
}

由于不创建一个新的变量保存引用每一次? (或者是编译足够的智能只是使用相同的变量)..

because a new variable to hold a reference is not created every time? (or is the complier intelligent enough just to use the same variable)..

(如果创建了新的变量是它malloced堆上?)

(If a new variable is created is it malloced on the heap?)

推荐答案

没有变量的存在几乎完全为程序员的缘故。你不是通过声明方法中的变量创建在运行时的任何额外的工作。

No, "variables" exist almost entirely for the sake of the programmer. You're not creating any additional work at run-time by declaring the variable inside the method.

在理论上,编译器会预留空间在栈上时调用一个方法在该方法中声明每个变量。因此在该方法该变量的presence将比其范围更重要。没有足够的空间分配上,除非使用了关键字堆。

In theory, the compiler will set aside space on the stack when a method is called for each variable declared in that method. So the presence of that variable in the method would be more important than its scope. No space is allocated on the heap unless the new keyword is used.

在实践中,编译器可以识别具有这样短的范围,它们可以存储在CPU而不是需要在堆栈上空间上的寄存器变量。例如:

In practice, the compiler can identify variables that have such a short scope that they can be stored in a register on the CPU instead of needing space on the stack. For example:

var a = b[c];
a.ToString();
// never access "a" again.

...将是相同的:

... would be the same as:

b[c].ToString();

...因为编译器识别它仅需要存储b并[c]的结果足够长以调用一个方法就可以了,因此它可以只使用而不是使用存储在CPU寄存器中。

... because the compiler recognizes that it only needs to store the result of b[c] long enough to call a method on it, so it can just use a CPU register instead of using memory.

由于这个原因,声明你的变量的的循环可能会导致实际的方法分配的的为变量的堆栈空间,这取决于可能的逻辑流之后。然而,进入庞大的微型优化,它没有任何意义为大多数人所关心的。

For this reason, declaring your variable inside the loop could actually cause the method to allocate less stack space for the variable, depending on the possible logic flow afterward. However, that gets into huge micro-optimization that doesn't make any sense for most people to care about.

由于有些人似乎仍然认为,宣布在一个循环的变量有一定的效果,我想我需要提供证据。键入下面的程序到LINQPad。

Since some people still seem to think that declaring a variable in a loop has some effect, I guess I need to provide proof. Type the following programs into LINQPad.

int j;
for(int i = 0; i < 5; i++)
{
    j = i;
}

......然后......

... and...

for(int i = 0; i < 5; i++)
{
    int j = i;
}

执行code,然后去IL标签来查看生成的IL code。这是相同的两个这些程序:

Execute the code, then go to the IL tab to see the generated IL code. It's the same for both of these programs:

IL_0000:  ldc.i4.0    
IL_0001:  stloc.0     
IL_0002:  br.s        IL_0008
IL_0004:  ldloc.0     
IL_0005:  ldc.i4.1    
IL_0006:  add         
IL_0007:  stloc.0     
IL_0008:  ldloc.0     
IL_0009:  ldc.i4.5    
IL_000A:  blt.s       IL_0004

因此​​,有无可辩驳的证据,这将使得在编译的时候没有任何区别。你会从两个方案得到完全相同的编译IL。

So there's incontrovertible proof that this will make no difference at compile time. You'll get exactly the same compiled IL from both programs.

这篇关于引用类型变量回收 - 是在一个循环中创建的每个环一个新的参考变量,如果其中的声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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