创建本地对象的价格 [英] Price of Creating Local Objects

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

问题描述

我有一个使用相当大的对象的方法。在方法中有一个本地对象或该方法使用的静态成员对象

之间的选择是




- -----选择1 ---------


int MyClass :: myMethod(int param){

MyBigObject o() ;

//基于参数初始化o

//用o

做计算返回calclulatedValue;

}


------选择2 ---------


int MyClass :: myMethod(int param) {

const MyBigObject& o = m_staticMemberHoldingAMyBigObject;

//基于param重新初始化

//休息与上面相同

}


每次调用myMethod()时,创建本地对象需要多少额外工作

?更确切地说,实际发生了什么?

每次为对象分配新内存?是否在堆栈上分配了内存

?在

方法调用期间存在的临时堆栈?我正在努力了解引擎盖下的内容,以及找到实际问题的答案。


选择也是如此1将是线程安全的,而选择2

不会?


感谢您的帮助,

cpp

解决方案

" cppaddict" <他*** @ hello.com>在消息中写道

news:bv ******************************** @ 4ax.com ...

我有一个使用相当大的对象的方法。选择是在方法中使用本地对象还是在方法使用的静态成员对象之间进行选择。


大到多大? 50个字节? 1千字节? 1兆字节?

------选择1 ---------

int MyClass :: myMethod(int param){
MyBigObject o();


声明名为o的方法不带参数并返回一个

MyBigObject。你可能意味着


MyBigObject o;

//基于param初始化o
//用o
返回calclulatedValue进行计算;
}
------选择2 ---------

int MyClass :: myMethod(int param){
const MyBigObject& o = m_staticMemberHoldingAMyBigObject;
//基于param重新初始化
// rest与上面相同


创建涉及多少额外工作每次调用myMethod()的本地对象
?更确切地说,实际发生了什么?
每次为对象分配新内存?是否在堆栈上分配了内存?在
方法调用期间存在的临时堆栈上?我正在努力了解引擎盖下的内容,以及找到实际问题的答案。


通常,在函数内本地声明的变量是快速分配的
。如果对象是怪异的,那么我可以看到一个

可能想要避免将其声明为函数的局部变量,但是

通常会处理<的问题;实现>堆栈空间< / implementation> ;.

我不会太担心实际发生的事情。相反,请集中讨论诸如它有多快?之类的问题。这很容易进行测量,并且b $ b确定有关效率问题的答案。如果您担心程序可能会失败,因为该对象不是类的静态成员,那么,无论如何,请参考实现。 b。 br />
选择1是否是线程安全的,而选择2
不是吗?




线程不是一部分标准C ++,但是,对于我可以想象的每个实现,第一个是线程安全的,第二个不是,除非你给
提供一个互斥锁。 />

-

David Hilsee


>大到多大? 50个字节? 1千字节? 1兆字节?


大约1千字节。

声明一个名为o的方法。不带参数并返回一个
MyBigObject。你可能意味着

MyBigObject o;


Woops。是的,这就是我的意思。

通常,在函数内本地声明的变量可以快速分配。如果对象是怪异的,那么我可以看到一个人如何避免将其声明为函数的局部变量,但是
通常会处理< implementation>堆栈空间的问题< / implementation> ;。
我不会太担心实际发生的事情。相反,请集中精力处理诸如它有多快?之类的问题。


我希望能感受到它有多快?通过了解

在幕后发生的事情。当然,我可以只是计时,看看,但是我没有洞察力。我真的很确定速度

不会成为一个问题,但只是因为我模糊地知道那些类似的代码很快就执行了。

如果您担心程序可能会失败,因为该对象不是类的静态成员,那么,请务必参考实现。


这不是一个问题。您认为它应该是什么原因?
线程不是标准C ++的一部分,但是,对于我可以想象的每个实现,第一个是线程安全的,第二个不是,除非你提供了一个互斥锁。




感谢您的帮助,

cpp


" cppaddict" <他*** @ hello.com>在消息中写道

news:tt ******************************** @ 4ax.com ...

有多大? 50个字节? 1千字节? 1兆字节?



大约1千字节。




这可能无需担心。我已经处理了C程序

,在几乎每个函数中,只为一个

临时sprintf()缓冲区分配了大约500个字节。

通常,在函数内本地声明的变量可以快速分配。如果对象是怪异的,那么我可以看到
如何避免将其声明为函数的局部变量,但是
通常会处理< implementation> stack
space<的问题。 /implementation>。我不会太担心实际发生的事情。相反,
集中讨论诸如它有多快?等问题。



我希望能够感受到它有多快?通过了解幕后发生的事情。当然,我可以及时看看,但是那时我没有洞察力。我其实非常确定速度不会成为问题,只是因为我模糊地知道类似的代码已经很快执行了。




如果你真的想知道幕后发生了什么,你需要检查实施情况。如果你对它执行的速度更快感兴趣,那么测量会更有帮助。一般来说,它是相对较快的
,因为局部变量通常会导致堆栈的一些小问题,这通常是一个廉价的操作。当然,任何评论

提到堆栈。指的是一个可能常见的实现。

如果您担心程序可能会失败,因为该对象不是静态的一个
类的成员,然后,请务必参考实现。



这不是一个问题。你有什么理由认为它应该是吗?




好​​吧,正如我之前提到的,可能有一个堆栈空间。耗尽问题,

但这对大多数应用程序来说通常不是问题。对于使用高递归算法的应用程序来说,这更像是一个问题。


-

David Hilsee


I have a method that uses a fairly large object. The choice is
between having a local object in the method or a static member object
that the method uses.

------CHOICE 1---------

int MyClass::myMethod(int param) {
MyBigObject o();
//initialize o based on param
//do calculations with o
return calclulatedValue;
}

------CHOICE 2---------

int MyClass::myMethod(int param) {
const MyBigObject& o = m_staticMemberHoldingAMyBigObject;
//re-initialzie o based on param
// rest is same as above
}

How much extra work is involved in the creation of the local object
with each call of myMethod()? More precisely, what actually happens?
Is new memory allocated for the object each time? Is the memory
allocated on the stack? On a temporary stack which exists during the
method call? I''m trying to understand what goes on under the hood as
well as find an answer to the practical problem.

Is it also true that choice 1 would be thread safe, while choice 2
would not?

Thanks for any help,
cpp

解决方案

"cppaddict" <he***@hello.com> wrote in message
news:bv********************************@4ax.com...

I have a method that uses a fairly large object. The choice is
between having a local object in the method or a static member object
that the method uses.
How large is large? 50 bytes? 1 kilobyte? 1 megabyte?
------CHOICE 1---------

int MyClass::myMethod(int param) {
MyBigObject o();
Declares a method named "o" that takes no arguments and returns a
MyBigObject. You probably meant

MyBigObject o;
//initialize o based on param
//do calculations with o
return calclulatedValue;
}

------CHOICE 2---------

int MyClass::myMethod(int param) {
const MyBigObject& o = m_staticMemberHoldingAMyBigObject;
//re-initialzie o based on param
// rest is same as above
}

How much extra work is involved in the creation of the local object
with each call of myMethod()? More precisely, what actually happens?
Is new memory allocated for the object each time? Is the memory
allocated on the stack? On a temporary stack which exists during the
method call? I''m trying to understand what goes on under the hood as
well as find an answer to the practical problem.
In general, variables that are declared locally within a function are
allocated quickly. If the object were monstrous, then I could see how one
might want to avoid declaring it as a local variable to a function, but that
generally deals with issues of <implementation>stack space</implementation>.
I wouldn''t worry too much about what actually happens. Instead, concentrate
on questions like "How fast is it?" It''s easy to take measurements and
determine answers for questions about efficiency. If you''re concerned that
the program might fail because the object is not a static member of a class,
then, by all means, consult the implementation.
Is it also true that choice 1 would be thread safe, while choice 2
would not?



Threads are not a part of standard C++, but, for every implementation that I
could imagine, the first is thread-safe and the second is not, unless you
provide a mutex.

--
David Hilsee


>How large is large? 50 bytes? 1 kilobyte? 1 megabyte?

About 1 kilobyte.

Declares a method named "o" that takes no arguments and returns a
MyBigObject. You probably meant

MyBigObject o;
Woops. Yes, that is what I meant.
In general, variables that are declared locally within a function are
allocated quickly. If the object were monstrous, then I could see how one
might want to avoid declaring it as a local variable to a function, but that
generally deals with issues of <implementation>stack space</implementation>.
I wouldn''t worry too much about what actually happens. Instead, concentrate
on questions like "How fast is it?"
I was hoping to get a feel for "How fast is it?" by understanding what
goes on behind the scenes. Of course, I could just time it and see,
but then I would have no insight. I''m actually pretty sure that speed
would not be an issue, but only because I vaguely know that vaguely
similar code has executed quickly.
If you''re concerned that
the program might fail because the object is not a static member of a class,
then, by all means, consult the implementation.
That is not a concern. Is there any reason you think it should be?
Threads are not a part of standard C++, but, for every implementation that I
could imagine, the first is thread-safe and the second is not, unless you
provide a mutex.



Thank for your help,
cpp


"cppaddict" <he***@hello.com> wrote in message
news:tt********************************@4ax.com...

How large is large? 50 bytes? 1 kilobyte? 1 megabyte?



About 1 kilobyte.



That''s probably nothing to be concerned about. I''ve dealt with C programs
that, in nearly every function, allocated around 500 bytes just for a
temporary sprintf() buffer.

In general, variables that are declared locally within a function are
allocated quickly. If the object were monstrous, then I could see how onemight want to avoid declaring it as a local variable to a function, but thatgenerally deals with issues of <implementation>stack space</implementation>.I wouldn''t worry too much about what actually happens. Instead, concentrateon questions like "How fast is it?"



I was hoping to get a feel for "How fast is it?" by understanding what
goes on behind the scenes. Of course, I could just time it and see,
but then I would have no insight. I''m actually pretty sure that speed
would not be an issue, but only because I vaguely know that vaguely
similar code has executed quickly.



If you really want to know exactly what is going on behind the scenes, you
should examine the implementation. If you''re more interested in how fast it
executes, then measurements will be more helpful. In general, it is
relatively quick, because local variables usually result in some fiddling
with the stack, which is usually a cheap operation. Of course, any comments
mentioning to a "stack" are referring to a possibly common implementation.

If you''re concerned that
the program might fail because the object is not a static member of a class,then, by all means, consult the implementation.



That is not a concern. Is there any reason you think it should be?



Well, as I mentioned before, there can be a "stack space" exhaustion issue,
but that''s not usually a problem for most applications. It''s more of a
problem for applications that use highly recursive algorithms.

--
David Hilsee


这篇关于创建本地对象的价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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