简单的代码性能问题 [英] simple code performance question

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

问题描述




鉴于此代码:


A ** ppA =新A * [10];

A * pA = NULL;

for(int i = 0; i< 10; ++ i)

{

pA = ppA [i];

//用pA做一些事情
}


- 如果pA有一些性能损失声明和作业

将在for-block中作为:


A * pA = ppA [i];


问候,

ren

Hi,

Given this code:

A** ppA = new A*[10];
A *pA = NULL;
for(int i = 0; i < 10; ++i)
{
pA = ppA[i];
//do something with pA
}

- is there some performance penalty if pA declaration and assignment
will be inside the for-block as:

A *pA = ppA[i];

Regards,
ren

推荐答案

ga ******** @ gmail.com 写道:

鉴于此代码:


A ** ppA =新A * [10];

A * pA = NULL;

for(int i = 0; i< 10; ++ i)

{

pA = ppA [i];

//用pA做点什么

}


- 如果pA声明和赋值

将在for-block中包含一些性能损失:


A * pA = ppA [i];
Given this code:

A** ppA = new A*[10];
A *pA = NULL;
for(int i = 0; i < 10; ++i)
{
pA = ppA[i];
//do something with pA
}

- is there some performance penalty if pA declaration and assignment
will be inside the for-block as:

A *pA = ppA[i];



cource不是。但是,声明/定义/初始化

在for-block中。代码维护POV要好得多。

外部''pA'变量的存在*没有优点*。它只会污染范围内的b
。除非需要在与'/ b $ b''ppA''相同的范围内使用''pA'',否则应在里面定义''pA'。


V

-

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

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

Of cource not. However, the declaration/definition/initialisation
inside "the for-block" is much better from the code maintenance POV.
Presence of ''pA'' variable outside has *no merit*. It only pollutes
the scope. Unless there is a need to use ''pA'' in the same scope as
''ppA'', ''pA'' should be defined inside.

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


感谢您的回答,但是您能否提供更多详情?

为什么会有这里没有性能损失,是因为它是一个

指针类型?

TIA

Oren

Thanks for the answer, but can you supply some more details?
Why there is no performance penalty here, is it because it is a
pointer type?
TIA
Oren


gl****@smile.net.il 写道:

感谢您的回答,但是您能提供更多详细信息吗?

为什么这里没有性能损失,是因为它是

指针类型?
Thanks for the answer, but can you supply some more details?
Why there is no performance penalty here, is it because it is a
pointer type?



是的。你在寻找什么细节?

让我们这样说:一个非常糟糕的编译器(它没有发出相同代码的

)这两种情况)可能会有一些差别,但是这样的话,b / b
微不足道,无论什么时候关于这个大的图片都没关系。

V

-

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

我不知道回复最热门的回复,请不要问

Yes. What details are you looking for?

Let''s say this: with a very bad compiler (which doesn''t emit the
same code in both cases) there might be some difference, but so
miniscule that it would not matter when the big[ger] picture is
concerned.

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


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

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