存储指针与存储对象 [英] storing pointer vs storing object

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

问题描述



STL中的所有容器都存在对象本身(因此需要复制

contsructability& assignability),而NTL或者提升

ptr_container存储指向容器中对象的指针(

独家拥有,或只是商店)。

现在,我的问题是关于何时使用的一般准则哪一个?

我的理解,

1)多态对象需要ptr_container。

2)非复制可构造,非可分配对象需要ptr_container。


除此之外,还有哪些指南?我特别感兴趣的是具有有限内存的小型设备(掌上电脑,掌上电脑等),其中获得

的性能很重要。

具体来说,它可以像一个指南,很多小对象=>

STL容器(由于缓存效果更快),一些大对象(比如

GUI小部件),使用ptr_container。那么,为什么应该有两种不同的

类型的容器,何时会使用其中一种?


谢谢

abir

Hi,
all of the containers in STL stores object itself (thus copy
contsructability & assignability is needed), while NTL or boost
ptr_container stores pointer to the object in the container (either
exclusively owns, or just stores).
Now, my question is for a general guideline when to use which one?
What I understand,
1) polymorphic objects need ptr_container.
2) non copy constructable, non assignable objects need ptr_container.

Other than that, any guideline is there? I am specially interested for
small devices (handheld, pda etc) with limited memory, where gaining
performance is important.
To be specific, can it be a guideline like, lots of small objects =>
STL container (faster due to cache effect)., a few large objects (like
GUI widgets) , use ptr_container. So, why there should be two different
types of container, and when one or the other would be used?

thanks
abir

推荐答案

toton写道:
toton wrote:

>



STL中的所有容器都存在对象本身(因此需要复制

contsructability& assignability),而NTL或boost

ptr_container存储指向容器中对象的指针(

独家拥有,或只是商店)。

现在,我的问题是一般指南何时使用哪一个?

我的理解,

1)多态对象需要ptr_container。

2)非复制可构造,不可分配的对象需要ptr_container。


除此之外,还有什么指南吗?我特别感兴趣的是具有有限内存的小型设备(掌上电脑,掌上电脑等),其中获得

的性能很重要。

具体来说,它可以像一个指南,很多小对象=>

STL容器(由于缓存效果更快),一些大对象(比如

GUI小部件),使用ptr_container。那么,为什么应该有两种不同的

类型的容器,何时会使用其中一种?


谢谢

abir
>
Hi,
all of the containers in STL stores object itself (thus copy
contsructability & assignability is needed), while NTL or boost
ptr_container stores pointer to the object in the container (either
exclusively owns, or just stores).
Now, my question is for a general guideline when to use which one?
What I understand,
1) polymorphic objects need ptr_container.
2) non copy constructable, non assignable objects need ptr_container.

Other than that, any guideline is there? I am specially interested for
small devices (handheld, pda etc) with limited memory, where gaining
performance is important.
To be specific, can it be a guideline like, lots of small objects =>
STL container (faster due to cache effect)., a few large objects (like
GUI widgets) , use ptr_container. So, why there should be two different
types of container, and when one or the other would be used?

thanks
abir






我认为存储原始数据的副本只对
有用
你需要非常快速地访问小值,正如你所提到的那样。

例如,在一个非常苛刻的循环中,storuing对象副本可能是一个好的
好想法,如果经常使用。因为访问指向数据

总是比直接访问副本慢。


经验法则我会说:


如果您关注的是内存优化速度合理 - >

指针/参考


如果您关心的是速度而且您有足够的内存和你存储

小对象-copy(因为大对象的副本可以是一个很慢的




干杯,

K

Hi,

Storing a copy of the original data is in my opinion only usefull when
you need to have very fast access to little values, as you mentionned.
For exemple in a very demanding loop, storuing object copies might be a
good idea, if those are often used. Because accessing to pointed data
is always slower than accessing directly to the copy.

A rule of thumb I''d say :

If your concern is memory optimization with reasonable speed ->
pointers/reference

If your concern is speed AND you have sufficient memory AND you store
little objects -copy ( because the copy of big objects can be a
little slow )

Cheers,
K




KiLVaiDeN写道:

KiLVaiDeN wrote:

toton写道:




STL中的所有容器都存储对象本身(因此复制

contsructability& assignability是必需的),而NTL或boost

ptr_container存储指向容器中对象的指针(

exclusive拥有,或只是商店。

现在,我的问题是一般准则何时使用哪一个?

我的理解,

1)多态对象需要ptr_container。

2)非复制构造,非可分配的对象需要ptr_container。


除此之外,任何指南都在那里吗?我特别感兴趣的是具有有限内存的小型设备(掌上电脑,掌上电脑等),其中获得

的性能很重要。

具体来说,它可以像一个指南,很多小对象=>

STL容器(由于缓存效果更快),一些大对象(比如

GUI小部件),使用ptr_container。那么,为什么应该有两种不同的

类型的容器,何时会使用其中一种?


谢谢

abir
toton wrote:

Hi,
all of the containers in STL stores object itself (thus copy
contsructability & assignability is needed), while NTL or boost
ptr_container stores pointer to the object in the container (either
exclusively owns, or just stores).
Now, my question is for a general guideline when to use which one?
What I understand,
1) polymorphic objects need ptr_container.
2) non copy constructable, non assignable objects need ptr_container.

Other than that, any guideline is there? I am specially interested for
small devices (handheld, pda etc) with limited memory, where gaining
performance is important.
To be specific, can it be a guideline like, lots of small objects =>
STL container (faster due to cache effect)., a few large objects (like
GUI widgets) , use ptr_container. So, why there should be two different
types of container, and when one or the other would be used?

thanks
abir






我认为存储原始数据的副本只对
有用
你需要非常快速地访问小值,正如你所提到的那样。

例如,在一个非常苛刻的循环中,storuing对象副本可能是一个好的
好想法,如果经常使用。因为访问指向数据

总是比直接访问副本慢。


经验法则我会说:


如果您关注的是内存优化速度合理 - >

指针/参考


如果您关心的是速度而且您有足够的内存和你存储

小对象-copy(因为大对象的副本可以是一个

有点慢)


Hi,

Storing a copy of the original data is in my opinion only usefull when
you need to have very fast access to little values, as you mentionned.
For exemple in a very demanding loop, storuing object copies might be a
good idea, if those are often used. Because accessing to pointed data
is always slower than accessing directly to the copy.

A rule of thumb I''d say :

If your concern is memory optimization with reasonable speed ->
pointers/reference

If your concern is speed AND you have sufficient memory AND you store
little objects -copy ( because the copy of big objects can be a
little slow )



除非另有证明,否则这两个问题都是次要的:




哪个选项产生最清晰的人类可读代码和

哪个选项可以让你达到代码的地步

更快地编写,修改和无错误


所以,对于OP:考虑该背景下的选项并选择

哪个最能回答这些问题。例如,如果您发现

使用指针容器编写正确的代码需要更长的时间(包括测试和调试时间),并且该代码不太清楚a

人类读者,然后使用容器的对象。


过早优化是所有邪恶的根源和所有这些。


Gavin Deane

Unless and until proven otherwise, both those concerns are secondary
to:

"Which option produces the most clearly human-readable code" and
"Which option enables you to get to the point where that code is
written, teted and error-free more quickly"

So, to the OP: consider the options in that context and choose
whichever answers those questions best. If you find, for example, that
it takes longer (including testing and debugging time) to write correct
code using containers of pointers, and that code is less clear to a
human reader, then use containers of objects.

Premature optimisation is the root of all evil and all that.

Gavin Deane


Gavin Deane写道:
Gavin Deane wrote:

>

除非另有证明,否则这两个问题都是次要的。




哪个选项产生最清晰的人类可读代码 ;和

哪个选项可以让你达到代码的地步

更快地编写,修改和无错误


所以,对于OP:考虑该背景下的选项并选择

哪个最能回答这些问题。例如,如果您发现

使用指针容器编写正确的代码需要更长的时间(包括测试和调试时间),并且该代码不太清楚a

人类读者,然后使用容器的对象。


过早优化是所有邪恶的根源和所有这些。


Gavin Deane
>
Unless and until proven otherwise, both those concerns are secondary
to:

"Which option produces the most clearly human-readable code" and
"Which option enables you to get to the point where that code is
written, teted and error-free more quickly"

So, to the OP: consider the options in that context and choose
whichever answers those questions best. If you find, for example, that
it takes longer (including testing and debugging time) to write correct
code using containers of pointers, and that code is less clear to a
human reader, then use containers of objects.

Premature optimisation is the root of all evil and all that.

Gavin Deane



我完全赞同你的观点,在正常的环境中,但是他的工作是小b $ b设备,因此它不仅仅是优化的问题,而是针对那些目标

平台的良好实践问题。


但是你所考虑的因素非常宝贵;最好的方法是测试两种情况,看看它是如何在你的目标

设备中管理,然后坚持一个或另一个;告诉我你是否同意:)


干杯,

K

I totally agree with your point, in a normal environnement, but he''s
working for small devices, therefore it''s not only a matter of
Optimization, but more a matter of good practices for those targeted
platforms.

The considerations you make are very valuable though; The best is to
test both of the scenarios, see how it''s managed in your targeted
device, and then sticking to one or another; Tell me if you agree :)

Cheers,
K


这篇关于存储指针与存储对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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