重复实例化变量/性能? [英] Repeated instantiation of a variable / performance?

查看:65
本文介绍了重复实例化变量/性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//代码序列A

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

int k = something();

//更多代码使用k

}


//代码序列B

int k ;

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

k = something();

// some更多代码使用k

}


两个序列在​​性能方面完全相同,还是重复了

序列B中k的实例化与序列B中的单个实例相比有一定的成本吗?

如果k属于类类型怎么样?

谢谢,罗伯特

解决方案

2004年7月2日星期五06:24:47 +0200,Robert Sturzenegger

< yo ************** @ ziksuhy.ch>写道:

//代码序列A
for(int i = 0; i< 10; ++ i){
int k = something() ;
//一些使用k的代码

//代码序列B
int k;
for(int i = 0; i < 10; ++ i){
k = something();
//一些使用k的代码

这两个序列是完全相同的在性能方面相同,或者具有顺序A中k重复实例的一定成本与序列B中的单个实例相比?
关于k是否属于班级类型?
谢谢,罗伯特




谁能说?一个编译器可能与下一个编译器不同。如果你真的感兴趣的是
然后编写程序并计时(或者查看

生成的机器代码)。就个人而言,我会惊讶地看到任何

的差异,但后来我从未真正研究过它。


至于班级版本,那么它取决于在课堂上(以及

编译器)。您正在将分配成本与复制成本进行比较

构造。哪个更有效率完全取决于课程是如何编写的,没有先验的理由期望更高效。


john


> //代码序列A

for(int i = 0; i< 10; ++ i){
int k = something();
//更多代码使用k

//代码序列B
int k;
for(int i = 0; i< 10; ++ i){
k = something();
//一些使用k的代码

两个序列在​​性能方面是完全相同的,还是具有<序列B中k的重复实例与序列B中的单个实例相比有一定的成本吗?


我在Windows上使用的编译器(Borland,Microsoft)在函数启动时为k分配了

空间。实际上有一个x86程序集

指令,所以你可以假设所有编写良好的编译器都会这样做。因此,对于简单类型,没有区别。

如果k是类类型怎么样?




正如John提到的那样,第一个是一个结构,第二个是作业。

在某些情况下,有些课程会做同样的事情。其他人将在构造案例中进行更多的初始化。一些人可能会在

分配案例中做更多工作。

我更喜欢建筑案例,因为

限制范围造成的清晰度,除非我确定对象的构造比作业贵得多。


Niels Dybdahl

< br>

>

正如约翰所提到的那样,第一个是构造,第二个是作业。
有些类在这些情况下也会这样做。其他人会在施工案例中进行更多的初始化。一些人可能会在
分配案例中做更多工作。


是的,我应该说OP正在比较重复复制的成本

建设和销毁,以及重复分配的成本。这是不可能的,因为任务的效率会降低。但是返回值是否优惠?b $ b优化在这里发挥作用?在我看来,编译器可能是

能够在复制构造案例中取消临时性。如果是这样的话,

就会把事情转回来支持复制结构。

我更喜欢建筑工程,因为范围有限,因为范围有限,除非我确切地知道对象
的构造比赋值要贵得多。




当然,一般来说,代码的清晰度是最重要的效率

保存所有。


john


// Code sequence A
for (int i = 0; i < 10; ++i) {
int k = something();
// some more code which uses k
}

// Code sequence B
int k;
for (int i = 0; i < 10; ++i) {
k = something();
// some more code which uses k
}

Are the two sequences exactly the same in terms of performance, or has the
repeated instanciation of k in sequence A a certain cost compared with the
single one instantiation in sequence B?
What about if k were of a class type?
Thanks, Robert

解决方案

On Fri, 2 Jul 2004 06:24:47 +0200, Robert Sturzenegger
<yo**************@ziksuhy.ch> wrote:

// Code sequence A
for (int i = 0; i < 10; ++i) {
int k = something();
// some more code which uses k
}

// Code sequence B
int k;
for (int i = 0; i < 10; ++i) {
k = something();
// some more code which uses k
}

Are the two sequences exactly the same in terms of performance, or has
the
repeated instanciation of k in sequence A a certain cost compared with
the
single one instantiation in sequence B?
What about if k were of a class type?
Thanks, Robert



Who can say? One compiler could be different from the next. If you are
really interested then write the program and time it (or look at the
generated machine code). Personally I would be surprised to see any
difference, but then I''ve never really looked into it.

As for the class version then it depends upon the class (and on the
compiler). You are comparing the cost of assignment with the cost of copy
construction. Which is more efficient depends entirely on how the class is
written, There is no a priori reason to expect either to be more efficient.

john


> // Code sequence A

for (int i = 0; i < 10; ++i) {
int k = something();
// some more code which uses k
}

// Code sequence B
int k;
for (int i = 0; i < 10; ++i) {
k = something();
// some more code which uses k
}

Are the two sequences exactly the same in terms of performance, or has the
repeated instanciation of k in sequence A a certain cost compared with the
single one instantiation in sequence B?
The compilers I have used (Borland, Microsoft) on Windows do allocate the
space for k when the function starts. Actually there is an x86 assembly
instruction for that, so you can assume that all wellwritten compilers do
that. So for simple types there will be no difference.
What about if k were of a class type?



As John mentions the first is a construction and the second an assignment.
Some classes will do the same in those cases. Others will do some more
initialization in the construction case. A few might do more work in the
assignment case.
I would prefer the construction case because of clarity caused by the
limited scope, unless I know for sure that the construction of the object is
much more expensive than an assignment.

Niels Dybdahl


>

As John mentions the first is a construction and the second an assignment.
Some classes will do the same in those cases. Others will do some more
initialization in the construction case. A few might do more work in the
assignment case.
Yes, I should have said the OP is comparing the cost of repeated copy
construction and destruction, with the cost of repeated assignment. It''s
unlikely that assignment will be less efficient. But does return value
optimization play a role here? It seems to me that the compiler might be
able to do away with a temporary in the copy construction case. If so that
would swing things back in favour of copy construction.
I would prefer the construction case because of clarity caused by the
limited scope, unless I know for sure that the construction of the object is much more expensive than an assignment.



Absolutely, in general clarity of code is the most important efficiency
saving of all.

john


这篇关于重复实例化变量/性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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