C ++智能指针表现 [英] C++ Smart Pointer performance

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

问题描述

多少使用智能指针呢,特别是提高::更多相比,在时间和内存方面的裸指针shared_ptr的成本是多少?使用裸指针更好的游戏/嵌入式系统的性能密集的地方?你会建议使用裸指针或智能指针性能密集型组件?

How much do using smart pointers, particularly boost::shared_ptr cost more compared to bare pointers in terms of time and memory? Is using bare pointers better for performance intensive parts of gaming/embedded systems? Would you recommend using bare pointers or smart pointers for performance intensive components?

推荐答案

提领智能指针通常是微不足道的,当然在释放模式升压。所有升压检查是在编译时。 (智能指针在理论上可以做跨线程聪明的东西)。这仍然留下了很多其他的操作。尼古拉提到建筑,复制和破坏。这不是一套完整的,但。其他重要的操作交换,转让和重置为NULL。基本上,任何操作,需要机灵。

Dereferencing smart pointers is typically trivial, certainly for boost in release mode. All boost checks are at compile-time. (Smart pointers could in theory do smart stuff across threads). This still leaves a lot of other operations. Nicola mentioned construction, copying and destruction. This is not the complete set, though. Other important operations are swapping, assignment and resetting to NULL. Basically, any operation that requires smartness.

请注意,其中一些业务是由一些智能指针之外。例如。 的boost :: scoped_ptr的甚至不能复制,更遑论被分配。作为这留下更少的操作,执行可以为这些较少的方法进行优化。

Note that some of these operations are excluded by some smart pointers. E.g. boost::scoped_ptr cannot even be copied, let alone be assigned. As this leaves less operations, the implementation can be optimized for these fewer methods.

在事实上,TR1来了,这是相当有可能的编译器可以使用智能指针比原始指针做的更好。例如。它可能是一个编译器可以证明一个聪明的不可复制的指针是不是在某些情况下别名,仅仅因为它是不可复制的。想想看:当创建了两个指针指向同一个对象时发生混淆。如果第一指针不能复制,如何将一个第二指针最终指向相同的对象? (有周围的方式,太 - 符*必须返回一个左值)

In fact, with TR1 coming up, it's quite likely that compilers can do better with smart pointers than raw pointers. E.g. it's possible that a compiler can prove that a smart non-copyable pointer is not aliased in some situations, merely because it's non-copyable. Think about it: aliasing occurs when two pointers are created pointing to the same object. If the first pointer cannot be copied, how would a second pointer end up pointing to the same object? (There are ways around that, too - operator* has to return an lvalue)

这篇关于C ++智能指针表现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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