块,或不? [英] Blocks, or not?
问题描述
从功能上讲,以下两段代码是相同的:
if(p == NULL){
break;
}
如果(p == NULL)
休息;
但是,我想知道是否有任何性能影响
选择一个而不是另一个。
定义一个块会产生额外的开销吗?编译器会将之前的代码优化为
后者吗?它甚至需要吗?
我问的原因是,我更愿意使用前代码,只需
因为我更喜欢外观。
" Rob" < IO ********* @ hotmail.com>在消息中写道
news:11 ********************** @ g49g2000cwa.googlegr oups.com ...在功能上,以下两段代码是相同的:
if(p == NULL){
break;
} >
如果(p == NULL)
休息;
但是,我想知道在选择其中一个是否有任何性能影响。
定义一个块会产生额外的开销吗?
前者可能需要几微秒才能编译,但我怀疑
很重要。
编译器是否会优化以前的代码到后者?它甚至需要
吗?
我会惊讶地发现任何为上面的替代品生成不同代码的编译器。
我问的原因是,我更喜欢使用前代码,只是因为我更喜欢外观。
FWIW,我可能会写:
if(!p)break;
再一次,我不希望编译时间有任何有意义的差异,并且
仍然生成与上述替代品相同的代码。
Alex
Rob在21/08/05写道:< blockquote class =post_quotes>在功能上,以下两段代码是相同的:
if(p == NULL){
break;
}
如果(p == NULL)
休息;
但是,我想知道在选择其中一个是否有任何性能影响
。
没有区别。
我个人推荐并使用第一个表格,方便
进行调试和扩展。
-
Emmanuel >
C-FAQ: http:/ /www.eskimo.com/~scs/C-faq/faq.html
C库: http://www.dinkumware.com/refxc.html
我曾经问过专家COBOL程序员,如何
在COBOL中声明局部变量,回复是:
什么是局部变量?
Rob写道:
从功能上讲,以下两段代码是相同的:
if(p == NULL){
休息;
}
如果(p == NULL)
休息;
然而,我想知道是否有任何性能影响
选择一个而不是另一个。
定义一个块会产生额外的开销吗?编译器会将以前的代码优化到后者吗?它甚至需要吗?
我问的原因是,
我更愿意选择以前的代码,只是因为我更喜欢外观。
我更喜欢使用带有所有ifs和elses以及循环的大括号。
它可以在修改代码时简化操作。
一旦修改代码,我犯了一个错误,如果我最初用括号编写代码,可以避免
。
我不记得是什么错误了。
-
pete
Functionally, the two following pieces of code are the same:
if(p == NULL) {
break;
}
if(p == NULL)
break;
However, I am wondering whether there are any performance implications
in choosing one over the other. Is there any extra overhead created by
defining a block? Will a compiler optimize the former code to the
latter? Does it even need to?
The reason I ask is, I''d much prefer to go with the former code, simply
because I prefer the appearance.
"Rob" <io*********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...Functionally, the two following pieces of code are the same:
if(p == NULL) {
break;
}
if(p == NULL)
break;
However, I am wondering whether there are any performance implications
in choosing one over the other. Is there any extra overhead created by
defining a block?
The former may take a few microseconds longer to compile, but I doubt that
is important.
Will a compiler optimize the former code to the latter? Does it even need
to?
I would be surprised to find any compiler which generated different code for
the alternatives above.
The reason I ask is, I''d much prefer to go with the former code, simply
because I prefer the appearance.
FWIW, I would probably write:
if (!p) break;
Again, I would not expect any meaningful difference in compilation time, and
still the same code generated as for the above alternatives.
Alex
Rob wrote on 21/08/05 :Functionally, the two following pieces of code are the same:
if(p == NULL) {
break;
}
if(p == NULL)
break;
However, I am wondering whether there are any performance implications
in choosing one over the other.
No difference.
I personnaly recommend the use of the first form, that is convenient
for debug and extensions.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
Rob wrote:
Functionally, the two following pieces of code are the same:
if(p == NULL) {
break;
}
if(p == NULL)
break;
However, I am wondering whether there are any performance implications
in choosing one over the other. Is there any extra overhead created by
defining a block? Will a compiler optimize the former code to the
latter? Does it even need to?
The reason I ask is,
I''d much prefer to go with the former code, simply
because I prefer the appearance.
I prefer to use braces with all ifs and elses and loops.
It can make things simpler when it''s time to modify the code.
Once, when modifying code, I made a mistake that could have
been avoided if I had originally written the code with braces.
I can''t remember what the mistake was.
--
pete
这篇关于块,或不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!