在循环中创建临时对象 [英] Creation of temporary objects in loop

查看:80
本文介绍了在循环中创建临时对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个效率更高(如果有的话)


[伪代码]

MyHugeClass MyObject;

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

{

MyObject = SomeVal [i];

使用MyObject;

}





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

{

MyHugeClass MyObject = SomeVal [i];

使用MyObject;

}


也就是说,如果我在for循环中声明MyObject,是否每次迭代都重新创建

,或者编译器是否创建它一次并重复使用?


或者它是UB吗?

Which is more efficient (if either)

[Pseudo code]
MyHugeClass MyObject;
for ( int i = 0; i < 1000; ++i )
{
MyObject = SomeVal[i];
Use MyObject;
}

or

for ( int i = 0; i < 1000; ++i )
{
MyHugeClass MyObject = SomeVal[i];
Use MyObject;
}

That is, if I declare MyObject inside a for loop, does it get recreated
each iteration, or does the compiler create it once and reuse each time?

Or is it UB?

推荐答案

* Jim Langston:
* Jim Langston:
这是更多高效(如果有的话)

[伪代码]
MyHugeClass MyObject;
for(int i = 0; i< 1000; ++ i)
{
MyObject = SomeVal [i];
使用MyObject;
}


for(int i = 0; i< 1000 ; ++ i)
{ MyHugeClass MyObject = SomeVal [i];
使用MyObject;
}
Which is more efficient (if either)

[Pseudo code]
MyHugeClass MyObject;
for ( int i = 0; i < 1000; ++i )
{
MyObject = SomeVal[i];
Use MyObject;
}

or

for ( int i = 0; i < 1000; ++i )
{
MyHugeClass MyObject = SomeVal[i];
Use MyObject;
}




在执行时间方面:取决于对象,所以,如果事实证明

是至关重要的,请测量。


就程序员时间而言:第一种通常效率更高。


一般情况下:在怀疑编译器可能能够做某事的情况下担心执行效率并不是一个好主意。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?



In terms of execution time: depends on the object, so if it turns out
to be critical, measure.

In terms of programmer time: the first is often hugely more efficient.

In general: it''s not a good idea to worry about execution efficiency in
cases where you suspect the compiler might be able to do something.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?




" Jim Langston" < TA ******* @ rocketmail.com>在消息中写道

新闻:wS ***************** @ fe05.lga ...

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:wS*****************@fe05.lga...
哪个更有效率(如果有的话)

[伪代码]
MyHugeClass MyObject;
for(int i = 0; i< 1000; ++ i)
{
MyObject = SomeVal [i];
使用MyObject;
}


for(int i = 0; i< 1000; ++ i)
MyHugeClass MyObject = SomeVal [i];
使用MyObject;
}


这取决于。但在大多数情况下,第一种情况会更有效,因为

赋值通常比复​​制构造函数轻。但也要考虑

ausing reference:


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

{

MyHugeClass& MyObject = SomeVal [i];

使用MyObject;

}

也就是说,如果我在for循环中声明MyObject,它是否会得到重新创建每次迭代,还是编译器创建一次并重复每次?


是的,它不仅重新创建,而且每次迭代都被破坏。

或者是UB?
Which is more efficient (if either)

[Pseudo code]
MyHugeClass MyObject;
for ( int i = 0; i < 1000; ++i )
{
MyObject = SomeVal[i];
Use MyObject;
}

or

for ( int i = 0; i < 1000; ++i )
{
MyHugeClass MyObject = SomeVal[i];
Use MyObject;
}
It depends. But in most cases the first case will be more efficient because
the assignment is usually lighter than a copy constructor. But also consider
ausing reference:

for (int = 0; i < 1000; ++i)
{
MyHugeClass& MyObject = SomeVal[i];
Use MyObject;
}

That is, if I declare MyObject inside a for loop, does it get recreated
each iteration, or does the compiler create it once and reuse each time?
yes, it is not only recreated but also destroyed each iteration.

Or is it UB?



* Alf P. Steinbach:
* Alf P. Steinbach:
* Jim Langston:
* Jim Langston:
哪个效率更高(如果有的话)

[伪代码]
MyHugeClass MyObject;
for(int i = 0; i< 1000; ++ i)
{
MyObject = SomeVal [i];
使用MyObject;
}


for(int i = 0; i< 1000; ++ i)
{
MyHugeClass MyObject = SomeVal [i];
使用MyObject;
}
在执行时间方面:取决于对象,所以如果结果是
关键,衡量。

就程序员时间而言:第一个通常效率更高。
Which is more efficient (if either)

[Pseudo code]
MyHugeClass MyObject;
for ( int i = 0; i < 1000; ++i )
{
MyObject = SomeVal[i];
Use MyObject;
}

or

for ( int i = 0; i < 1000; ++i )
{
MyHugeClass MyObject = SomeVal[i];
Use MyObject;
}
In terms of execution time: depends on the object, so if it turns out
to be critical, measure.

In terms of programmer time: the first is often hugely more efficient.




我当然是指_second_。


重置一个对象通常很难,并且很难依赖于对象状态的任何内容。


异常是指对象具有自然默认状态,例如作为

空字符串或空向量,但即使存在构造成本

+破坏,相对于赋值/重置,通常可以忽略不计。


一般情况下:在怀疑编译器可能做某事的情况下担心执行效率并不是一个好主意。



I meant the _second_, of course.

"Resetting" an object is often difficult and makes it difficult to rely
on anything about the object state.

An exception is where an object has a natural default state such as an
empty string or empty vector, but even there the cost of construction
+ destruction, relative to assignment/reset, is typically negligible.

In general: it''s not a good idea to worry about execution efficiency in
cases where you suspect the compiler might be able to do something.




-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于在循环中创建临时对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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