非常简单的问题 [英] very simple question

查看:75
本文介绍了非常简单的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我只是想知道

中的性能是否存在差异,在函数开头或内部声明变量对于

循环。例如:


双重测试()

{

双倍a,b,c;

for(int i = 0; i< n; i ++){

a = i * 10;

b = ai;

c + = a * b;

}

返回c;

}


或:


双重测试()

{

双c;


for(int i = 0 ; i< n; i ++){

double a = i * 10;

double b = ai;

c + = a * b;

}

返回c;

}

Hi everyone,

I just wanted to know if there is any difference in performance in
declarating the variables in the beginning of a function or within for
loops. For example:

double test()
{
double a,b,c;
for(int i=0; i<n; i++){
a = i*10;
b = a-i;
c += a*b;
}
return c;
}

or:

double test()
{
double c;

for(int i=0; i<n; i++){
double a = i*10;
double b = a-i;
c += a*b;
}
return c;
}

推荐答案

aaragon写道:
aaragon wrote:

我只想知道

中是否有任何性能差异在开头声明变量一个函数或者在
循环中。 [..]
I just wanted to know if there is any difference in performance in
declarating the variables in the beginning of a function or within for
loops. [..]



在实际计算两种功能之前无法知道。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要''请问

There is no way to know before actually clocking both functions.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


aaragon写道:
aaragon wrote:

double test()

{

双c;


for(int i = 0; i< n; i ++){

double a = i * 10 ;

双b = ai;

c + = a * b;

}

返回c;

}
double test()
{
double c;

for(int i=0; i<n; i++){
double a = i*10;
double b = a-i;
c += a*b;
}
return c;
}



Google过早优化。总是写出最容易让b
程序员理解的版本。你的第二个版本使得a和b'的角色清晰。


而且,在可变位置的水平上,几乎没有什么可以做的

更好而不是编译器会这样做。编译器可能会优化一个

和b几乎消失,并且只会使用FPU寄存器,无论你在哪里
声明它们。


-

Phlip
http ://flea.sourceforge.net/PiglegToo_1.html


" aaragon" < al ************** @ gmail.comwrote in message

news:11 **************** ******@l77g2000hsb.googlegr oups.com ...
"aaragon" <al**************@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...

大家好,


我只是想要要知道

中的性能是否有任何差异,在函数开头声明变量或在
循环内声明变量。例如:


双重测试()

{

双倍a,b,c;

for(int i = 0; i< n; i ++){

a = i * 10;

b = ai;

c + = a * b;

}

返回c;

}


或:


双重测试()

{

双c;


for(int i = 0 ; i< n; i ++){

double a = i * 10;

double b = ai;

c + = a * b;

}

返回c;

}
Hi everyone,

I just wanted to know if there is any difference in performance in
declarating the variables in the beginning of a function or within for
loops. For example:

double test()
{
double a,b,c;
for(int i=0; i<n; i++){
a = i*10;
b = a-i;
c += a*b;
}
return c;
}

or:

double test()
{
double c;

for(int i=0; i<n; i++){
double a = i*10;
double b = a-i;
c += a*b;
}
return c;
}



考虑n == 0.如果n == 0为你的第二个例子,varibles将

永远不必分配。


如果它们总是被分配(n 0)然后差别是分配

..vs。初始化。对于简单类型(double,float等等),它可能

并没有太大的区别。但是,对于类,如果他们有需要时间的构造函数,那就可以了。


C ++中的一般规则是在使用它们之前的delcare变量。一个

的主要原因是你知道变量的声明。如果

函数很大而且你遇到了

a = 10;

什么是?是int吗?双?炭?一类?你必须向上滚动到

才能找到函数的顶部。然而

加倍a = 10;

使得更容易阅读/维护代码IMO。

consider if n == 0. if n==0 for your second example the varibles would
never have to be allocated.

If they will always be allocated (n 0) then the difference is assignment
..vs. initialization. For simple types (double, float, etc...) it probably
doesn''t make much difference. For classes, however, it can if they have
constructors that take time.

The general rule in C++ is delcare varaibles just before you use them. One
of the main reasons is so you know what the variable is declared. If a
function is large and you come across
a = 10;
what is a? Is it int? double? char? a class? You''d have to scroll up to
the top of the function to find out. However
double a = 10;
makes it easier to read/maintain the code IMO.


这篇关于非常简单的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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