循环中的嵌套变量 - 效率低下? [英] nested variables in loops - inefficient?

查看:79
本文介绍了循环中的嵌套变量 - 效率低下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在块中使用局部声明通常会使代码更具可读性,但

效率会降低吗?例如...


void P(){

while(...){

int i = ... ;

bool b = ...;

....

}

}


乍一看,这看起来效率低下,因为每次循环时i和b都被分配

。但我想知道是不是真的如此。我听说当程序P放在堆栈上时,P中的所有*变量

,包括嵌套范围内的那些,也被分配。即。在

上面的例子中,我和b只会在

程序开始时分配一次。但是,我不确定这是不是真的。


如果变量嵌套在条件中怎么办 - 它仍然总是分配给b / b
开始?例如...


while(...){

if(...){

int i =。 ..;

bool b = ...;

....

}

}

这里的任何专家都可以让我放松一下

嵌套变量的可能效率吗?


有点背景 - 我''在写一个实时应用程序时,有几个

复杂的循环处理数据正以高速率接收,所以我继续遇到这个困境。


TIA,


Javaman

解决方案

无论*哪里*变量位于方法中:它们都是

仅分配*一次*。

但是如果你给它们赋值,例如int i = 0;每次执行这行代码时都会发生分配




" Javaman59" < JA ******* @ discussions.microsoft.com> schrieb im Newsbeitrag

新闻:4F ********************************** @ microsof t.com ...

在块中使用局部声明通常会使代码更具可读性,但
效率会降低吗?例如...

void P(){
while(...){
int i = ...;
bool b = ...;
....
}

乍一看,这看起来效率低下,因为每次循环时都会分配i和b。但我想知道是不是真的如此。我已经听说过,当程序P放在堆栈上时,P中的所有*变量
,包括嵌套范围内的变量,也会被分配。即。
在上面的例子中,我和b只会在
程序开始时分配一次。但是,我不确定这是不是真的。

如果变量嵌套在条件中会怎么样 - 它在开始时是否仍然总是被分配?例如...

while(...){
if(...){
int i = ...;
bool b = .. 。
....
}
}

这里的任何专家都可以让我放松关于嵌套变量可能存在的不足之处?复杂的循环处理数据正在以高速率接收,所以我不断遇到这个dilemna。

TIA,

Javaman



您的效率问题的答案是它取决于。在发布版本中,编译器优化了代码,它将在循环范围之外移动int

。对于价值类型而言,它确实无关紧要,因为无论如何它们都会在堆栈中。但是对于参考类型,循环中的每个时间都会导致对象被释放,因此在创建新对象时可能会产生GC。因此我不会这样做。

字符串的东西。


然而更重要的问题是该变量的初始化。

鉴于您的代码,您是否希望每次通过

循环进行本地访问?如果是这样,那么分配必须在每次迭代期间发生,即使对象创建只发生一次




通过将var移到循环外部,你也会影响能见度

var的范围。你不可能宣布另一个我。在这个函数中

如果它被移到循环之外。但是,如果它仍然在循环中,那么你可以重复使用i。在其他循环中。


编译器非常擅长优化像这样的简单案例。

编译器将移动任何循环不变的代码超出

循环的范围。因此,我不担心你提到的一般情况,除非

你正在处理参考类型。


就个人而言,我总是把局部变量放在外面循环,但这实际上是我用C ++编写实时编译器时的保留。这不是可读的,而是感觉更快。对我来说。


Michael Taylor - 9/19/05


" Javaman59"写道:

在块中使用局部声明通常会使代码更具可读性,但
效率会降低吗?例如...

void P(){
while(...){
int i = ...;
bool b = ...;
....
}

乍一看,这看起来效率低下,因为每次循环时都会分配i和b。但我想知道是不是真的如此。我已经听说过,当程序P放在堆栈上时,P中的所有*变量
,包括嵌套范围内的变量,也会被分配。即。在上面的例子中,在
程序开始时,i和b只会被分配一次。但是,我不确定这是不是真的。

如果变量嵌套在条件中怎么办?它仍然总是在开始时分配?例如...

while(...){
if(...){
int i = ...;
bool b = .. 。
....
}
}

这里的任何专家都可以让我放松关于嵌套变量可能存在的不足之处?复杂的循环处理数据正在以高速率接收,所以我不断遇到这个dilemna。

TIA,

Javaman



Javaman59< Ja ****** *@discussions.microsoft.com>写道:

在块中使用局部声明通常会使代码更具可读性,但
效率会降低吗?例如...

void P(){
while(...){
int i = ...;
bool b = ...;
....
}
}




在下面,P()和Q()编译成确切的同样的IL对我来说(和

优化是*关*)。您可以使用ILDASM自己尝试。


使用系统;


类测试

{

static void Main(){

P();

Q();

}

static void P(){

while(true){

int i = Console.Read();

bool b = i == 5;

Console.WriteLine(i);

Console.WriteLine(b);

}

}


静态无效Q(){

int i;

bool b;

while(true){

i = Console.Read();

b = i == 5;

Console.WriteLine(i );

Console.WriteLine(b);

}

}

}


Using local declarations within a block often makes code more readable, but
is it less efficient? eg...

void P() {
while (...) {
int i = ...;
bool b = ...;
....
}
}

At first sight this looks inefficient, because i and b are being allocated
each time around the loop. But I wonder if that is really the case. I have
heard that when procedure P is placed on the stack, that *all* variables
within P, including those within a nested scope, are also allocated. ie. in
the above example, i and b would be allocated only once, at the start of the
procedure. However, I am not sure whether this is the true.

What if the the variable is nested within a conditional - is it still always
allocated at the start? eg...

while (...) {
if (...) {
int i = ...;
bool b = ...;
....
}
}
Any experts here who can ease my mind about the possible ineffiency of
nested variables?

A bit of background - I''m writing a real time application, with several
complex loops processing data which is being received at a high rate, so I
keep coming across this dilemna.

TIA,

Javaman

解决方案

No matter *where* the variables are located in the method: they all are
allocated only *once*.
But if you assign a value to them, e.g. int i=0; the assignment takes place
everytime this line of code is executed.

"Javaman59" <Ja*******@discussions.microsoft.com> schrieb im Newsbeitrag
news:4F**********************************@microsof t.com...

Using local declarations within a block often makes code more readable,
but
is it less efficient? eg...

void P() {
while (...) {
int i = ...;
bool b = ...;
....
}
}

At first sight this looks inefficient, because i and b are being allocated
each time around the loop. But I wonder if that is really the case. I have
heard that when procedure P is placed on the stack, that *all* variables
within P, including those within a nested scope, are also allocated. ie.
in
the above example, i and b would be allocated only once, at the start of
the
procedure. However, I am not sure whether this is the true.

What if the the variable is nested within a conditional - is it still
always
allocated at the start? eg...

while (...) {
if (...) {
int i = ...;
bool b = ...;
....
}
}
Any experts here who can ease my mind about the possible ineffiency of
nested variables?

A bit of background - I''m writing a real time application, with several
complex loops processing data which is being received at a high rate, so I
keep coming across this dilemna.

TIA,

Javaman



There answer to your efficiency question is that it depends. In a release
build where the compiler optimizes your code it will defintely move the int
outside the scope of the loop. For value types it really doesn''t matter
because they''ll be on the stack anyway. For reference types however each
time through the loop would cause the object to be release and therefore
potentially GC''ed while a new one is created. Thus I wouldn''t do this sort
of thing for strings.

The more important question however is the initialization of that var.
Given your code do you expect the local to be inited each time through the
loop? If so then the assignment must happen during each iteration even if
the object creation happens only once.

By moving the var outside the loop you are also impacting the visibility
scope of the var. You won''t be able to declare another "i" in this function
if it is moved outside the loop. However if it remains in the loop then you
can reuse "i" in other loops.

Compilers are really good at optimizing simple cases like this one. The
compiler will move any code that is loop invariant outside the scope of the
loop. Therefore I wouldn''t worry about the general case you mentioned unless
you''re dealing with reference types.

Personally I always put local vars outside the loop but this is really a
holdover from my days of writing real-time compilers in C++. It''s not as
readable but it feels "faster" to me.

Michael Taylor - 9/19/05

"Javaman59" wrote:

Using local declarations within a block often makes code more readable, but
is it less efficient? eg...

void P() {
while (...) {
int i = ...;
bool b = ...;
....
}
}

At first sight this looks inefficient, because i and b are being allocated
each time around the loop. But I wonder if that is really the case. I have
heard that when procedure P is placed on the stack, that *all* variables
within P, including those within a nested scope, are also allocated. ie. in
the above example, i and b would be allocated only once, at the start of the
procedure. However, I am not sure whether this is the true.

What if the the variable is nested within a conditional - is it still always
allocated at the start? eg...

while (...) {
if (...) {
int i = ...;
bool b = ...;
....
}
}
Any experts here who can ease my mind about the possible ineffiency of
nested variables?

A bit of background - I''m writing a real time application, with several
complex loops processing data which is being received at a high rate, so I
keep coming across this dilemna.

TIA,

Javaman



Javaman59 <Ja*******@discussions.microsoft.com> wrote:

Using local declarations within a block often makes code more readable, but
is it less efficient? eg...

void P() {
while (...) {
int i = ...;
bool b = ...;
....
}
}



In the following, P() And Q() compile down to the exact same IL for me (and
optimization is *off*). You can try it yourself with ILDASM.

using System;

class Test
{
static void Main() {
P();
Q();
}

static void P() {
while (true) {
int i = Console.Read();
bool b = i == 5;
Console.WriteLine(i);
Console.WriteLine(b);
}
}

static void Q() {
int i;
bool b;
while (true) {
i = Console.Read();
b = i == 5;
Console.WriteLine(i);
Console.WriteLine(b);
}
}
}


这篇关于循环中的嵌套变量 - 效率低下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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