- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这周我发现了 boost::object_pool 并且惊讶于它比普通的新建和删除快了大约 20-30%。
为了测试,我编写了一个小型 C++ 应用程序,它使用 boost::chrono 为不同的堆分配器/释放器 (shared_ptr) 计时。这些函数本身使用“新建”和“删除”进行 60M 次迭代的简单循环。代码下方:
#include <iostream>
#include <memory>
using std::shared_ptr;
#include <boost/smart_ptr.hpp>
#include <boost/chrono.hpp>
#include <boost/chrono/chrono_io.hpp>
#include <boost/pool/object_pool.hpp>
#include <SSVUtils/SSVUtils.h>
#include "TestClass.h"
const long lTestRecursion = 60000000L;
void WithSmartPtrs()
{
boost::chrono::system_clock::time_point startTime = boost::chrono::system_clock::now();
std::cout << "Start time: " << startTime << std::endl;
for (long i=0; i < lTestRecursion; ++i)
{
boost::shared_ptr<TestClass> spTC = boost::make_shared<TestClass>("Test input data!");
}
boost::chrono::system_clock::time_point endTime = boost::chrono::system_clock::now();
std::cout << "End time: " << endTime << std::endl;
boost::chrono::duration<double> d = endTime - startTime;
std::cout << "Duration: " << d << std::endl;
}
void WithSTDSmartPtrs()
{
boost::chrono::system_clock::time_point startTime = boost::chrono::system_clock::now();
std::cout << "Start time: " << startTime << std::endl;
for (long i=0; i < lTestRecursion; ++i)
{
std::shared_ptr<TestClass> spTC = std::make_shared<TestClass>("Test input data!");
}
boost::chrono::system_clock::time_point endTime = boost::chrono::system_clock::now();
std::cout << "End time: " << endTime << std::endl;
boost::chrono::duration<double> d = endTime - startTime;
std::cout << "Duration: " << d << std::endl;
}
template<typename T> struct Deleter {
void operator()(T *p)
{
delete p;
}
};
void WithSmartPtrsUnique()
{
boost::chrono::system_clock::time_point startTime = boost::chrono::system_clock::now();
std::cout << "Start time: " << startTime << std::endl;
for (long i=0; i < lTestRecursion; ++i)
{
boost::unique_ptr<TestClass, Deleter<TestClass> > spTC = boost::unique_ptr<TestClass, Deleter<TestClass> >(new TestClass("Test input data!"));
}
boost::chrono::system_clock::time_point endTime = boost::chrono::system_clock::now();
std::cout << "End time: " << endTime << std::endl;
boost::chrono::duration<double> d = endTime - startTime;
std::cout << "Duration: " << d << std::endl;
}
void WithSmartPtrsNoMakeShared()
{
boost::chrono::system_clock::time_point startTime = boost::chrono::system_clock::now();
std::cout << "Start time: " << startTime << std::endl;
for (long i=0; i < lTestRecursion; ++i)
{
boost::shared_ptr<TestClass> spTC = boost::shared_ptr<TestClass>( new TestClass("Test input data!"));
}
boost::chrono::system_clock::time_point endTime = boost::chrono::system_clock::now();
std::cout << "End time: " << endTime << std::endl;
boost::chrono::duration<double> d = endTime - startTime;
std::cout << "Duration: " << d << std::endl;
}
void WithoutSmartPtrs()
{
boost::chrono::system_clock::time_point startTime = boost::chrono::system_clock::now();
std::cout << "Start time: " << startTime << std::endl;
for (long i=0; i < lTestRecursion; ++i)
{
TestClass* pTC = new TestClass("Test input data!");
delete pTC;
}
boost::chrono::system_clock::time_point endTime = boost::chrono::system_clock::now();
std::cout << "End time: " << endTime << std::endl;
boost::chrono::duration<double> d = endTime - startTime;
std::cout << "Duration: " << d << std::endl;
}
void WithObjectPool()
{
boost::chrono::system_clock::time_point startTime = boost::chrono::system_clock::now();
std::cout << "Start time: " << startTime << std::endl;
{
boost::object_pool<TestClass> pool;
for (long i=0; i < lTestRecursion; ++i)
{
TestClass* pTC = pool.construct("Test input data!");
pool.destroy(pTC);
}
}
boost::chrono::system_clock::time_point endTime = boost::chrono::system_clock::now();
std::cout << "End time: " << endTime << std::endl;
boost::chrono::duration<double> d = endTime - startTime;
std::cout << "Duration: " << d << std::endl;
}
void WithObjectPoolNoDestroy()
{
boost::chrono::system_clock::time_point startTime = boost::chrono::system_clock::now();
std::cout << "Start time: " << startTime << std::endl;
//{
boost::object_pool<TestClass> pool;
for (long i=0; i < lTestRecursion; ++i)
{
TestClass* pTC = pool.construct("Test input data!");
//pool.destroy(pTC);
}
//}
boost::chrono::system_clock::time_point endTime = boost::chrono::system_clock::now();
std::cout << "End time: " << endTime << std::endl;
boost::chrono::duration<double> d = endTime - startTime;
std::cout << "Duration: " << d << std::endl;
}
void WithSSVUtilsPreAllocDyn()
{
boost::chrono::system_clock::time_point startTime = boost::chrono::system_clock::now();
std::cout << "Start time: " << startTime << std::endl;
{
ssvu::PreAlloc::PreAllocDyn preAllocatorDyn(1024*1024);
for (long i=0; i < lTestRecursion; ++i)
{
TestClass* pTC = preAllocatorDyn.create<TestClass>("Test input data!");
preAllocatorDyn.destroy(pTC);
}
}
boost::chrono::system_clock::time_point endTime = boost::chrono::system_clock::now();
std::cout << "End time: " << endTime << std::endl;
boost::chrono::duration<double> d = endTime - startTime;
std::cout << "Duration: " << d << std::endl;
}
void WithSSVUtilsPreAllocStatic()
{
boost::chrono::system_clock::time_point startTime = boost::chrono::system_clock::now();
std::cout << "Start time: " << startTime << std::endl;
{
ssvu::PreAlloc::PreAllocStatic<TestClass> preAllocatorStat(10);
for (long i=0; i < lTestRecursion; ++i)
{
TestClass* pTC = preAllocatorStat.create<TestClass>("Test input data!");
preAllocatorStat.destroy(pTC);
}
}
boost::chrono::system_clock::time_point endTime = boost::chrono::system_clock::now();
std::cout << "End time: " << endTime << std::endl;
boost::chrono::duration<double> d = endTime - startTime;
std::cout << "Duration: " << d << std::endl;
}
int main()
{
std::cout << " With OUT smartptrs (new and delete): " << std::endl;
WithoutSmartPtrs();
std::cout << std::endl << " With smartptrs (boost::shared_ptr withOUT make_shared): " << std::endl;
WithSmartPtrsNoMakeShared();
std::cout << std::endl << " With smartptrs (boost::shared_ptr with make_shared): " << std::endl;
WithSmartPtrs();
std::cout << std::endl << " With STD smart_ptr (std::shared_ptr with make_shared): " << std::endl;
WithSTDSmartPtrs();
std::cout << std::endl << " With Object Pool (boost::object_pool<>): " << std::endl;
WithObjectPool();
std::cout << std::endl << " With Object Pool (boost::object_pool<>) but without destroy called!: " << std::endl;
WithObjectPoolNoDestroy();
std::cout << std::endl << " With SSVUtils PreAllocDyn(1024*1024)!: " << std::endl;
WithSSVUtilsPreAllocDyn();
std::cout << std::endl << " With SSVUtils PreAllocStatic(10)!: " << std::endl;
WithSSVUtilsPreAllocStatic();
return 0;
}
结果:
On Ubuntu LTS 12.04 x64 with GNU C++ 4.6 and boost 1.49
No smart ptrs (new/delete) 5,08024 100 5,1387 100 5,1108 100 5,1099 100
With boost::shared_ptr No boost::make_shared 7,36128 2,2810 145 7,34522 2,2065 143 7,28801 2,1772 143 7,3315 143
With boost::shared_ptr and boost::make_shared 6,60351 1,5233 130 6,82849 1,6898 133 6,61059 1,4998 129 6,6809 131
With std::shared_ptr and std::make_shared 6,07756 0,9973 120 5,93100 0,7923 115 5,9037 0,7929 116 5,9708 117
With boost::unique_ptr 4,97147 -0,1088 100 5,0428 -0,0959 98 4,96625 -0,1445 97 4,9935 98
With boost::object_pool 3,53291 -1,5473 70 3,60357 -1,5351 70 3,52986 -1,5809 69 3,5554 70
With boost::object_pool (Without calling Destroy) 4,52430 -0,5559 89 4,51602 -0,6227 88 4,52137 -0,5894 88 4,5206 88
在我的 MacBook Pro 上包含 SSVUtils PreAllocDyn 的结果:编译:
g++-mp-4.8 -I$BOOSTHOME/include -I$SSVUTILSHOME/include -std=c++11 -O2 -L$BOOSTHOME/lib -lboost_system -lboost_chrono -o smartptrtest smartptr.cpp
With OUT smartptrs (new and delete):
Start time: 1381596718412786000 nanoseconds since Jan 1, 1970
End time: 1381596731642044000 nanoseconds since Jan 1, 1970
Duration: 13.2293 seconds
With smartptrs (boost::shared_ptr withOUT make_shared):
Start time: 1381596731642108000 nanoseconds since Jan 1, 1970
End time: 1381596753651561000 nanoseconds since Jan 1, 1970
Duration: 22.0095 seconds
With smartptrs (boost::shared_ptr with make_shared):
Start time: 1381596753651611000 nanoseconds since Jan 1, 1970
End time: 1381596768909452000 nanoseconds since Jan 1, 1970
Duration: 15.2578 seconds
With STD smart_ptr (std::shared_ptr with make_shared):
Start time: 1381596768909496000 nanoseconds since Jan 1, 1970
End time: 1381596785500599000 nanoseconds since Jan 1, 1970
Duration: 16.5911 seconds
With Object Pool (boost::object_pool<>):
Start time: 1381596785500638000 nanoseconds since Jan 1, 1970
End time: 1381596793484515000 nanoseconds since Jan 1, 1970
Duration: 7.98388 seconds
With Object Pool (boost::object_pool<>) but without destroy called!:
Start time: 1381596793484551000 nanoseconds since Jan 1, 1970
End time: 1381596805774318000 nanoseconds since Jan 1, 1970
Duration: 12.2898 seconds
With SSVUtils PreAllocDyn(1024*1024)!:
Start time: 1381596815742696000 nanoseconds since Jan 1, 1970
End time: 1381596824173405000 nanoseconds since Jan 1, 1970
Duration: 8.43071 seconds
With SSVUtils PreAllocStatic(10)!:
Start time: 1381596824173448000 nanoseconds since Jan 1, 1970
End time: 1381596832034965000 nanoseconds since Jan 1, 1970
Duration: 7.86152 seconds
我的问题:除了 shared_ptr/unique_ptr/boost::object_pool 之外,是否还有更多堆/分配机制可用于快速堆分配/取消分配大型对象集?
注意:我在其他机器和操作系统上也有更多结果。
编辑 1:添加了 SSVUtils PreAllocDyn 结果编辑 4:添加了我的编译器命令行选项并使用 SSVUtils PreAllocStatic(10) 重新测试
谢谢
最佳答案
当我需要一个快速的新建/删除机制时,我自己编写了它。我不得不妥协“通用动态分配内存”的要求。这种改进使我能够准确地编写我需要的代码。简而言之——
思路很简单——
MyType preMyType[1000]
我将所有内容打包到一个漂亮、简单易用的框架中,对用户的要求很少。它最终派生自某个类并声明初始大小。如果您愿意,我可以详细说明,包括代码示例。
关于c++ - 是否有比 boost::object_pool 更快的 C++ 堆分配/释放机制可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19333595/
广播的原则 如果两个数组的后缘维度(从末尾开始算起的维度)的轴长度相符或其中一方的长度为1,则认为它们是广播兼容的。广播会在缺失维度和(或)轴长度为1的维度上进行。 在上面的对arr每一列减去列
之前在讲 MySQL 事务隔离性提到过,对于写操作给读操作的影响这种情形下发生的脏读、不可重复读、虚读问题。是通过MVCC 机制来进行解决的,那么MVCC到底是如何实现的,其内部原理是怎样的呢?我们要
我创建了一个 JavaScript 对象来保存用户在 ColorBox 中检查复选框时设置的值。 . 我对 jQuery 和“以正确的方式”编程 JavaScript 比较陌生,希望确保以下用于捕获用
我为了回答aquestion posted here on SO而玩示例,发现很难理解python的import *破坏作用域的机制。 首先是一点上下文:这个问题不涉及实际问题;我很清楚from fo
我想让我的类具有标识此类的参数 ID。例如我想要这样的东西: class Car { public static virtual string ID{get{return "car";}} }
更新:我使用的是 Java 1.6.34,没有机会升级到 Java 7。 我有一个场景,我每分钟只能调用一个方法 80 次。它实际上是由第 3 方编写的服务 API,如果您多次调用它,它会“关闭”(忽
希望这对于那些使用 Javascript 的人来说是一个简单的答案...... 我有一个日志文件,该文件正在被一个脚本监视,该脚本将注销中的新行提供给任何连接的浏览器。一些人评论说,他们希望看到的更多
我们正在开发针对 5.2 开发的 PHP 应用程序,但我们最近迁移到了 PHP 5.3。我们没有时间去解决所有迁移到 PHP 5.3 的问题。具体来说,我们有很多消息: Declaration of
简介 在实现定时调度功能的时候,我们往往会借助于第三方类库来完成,比如: quartz 、 spring schedule 等等。jdk从1.3版本开始,就提供了基于 timer 的定时调度功能。
Java中,一切都是对象,在分布式环境中经常需要将Object从这一端网络或设备传递到另一端。这就需要有一种可以在两端传输数据的协议。Java序列化机制就是为了解决这个问题而
我将编写自己的自定义控件,它与 UIButton 有很大不同。由于差异太大,我决定从头开始编写。所以我所有的子类都是 UIControl。 当我的控件在内部被触摸时,我想以目标操作的方式触发一条消息。
在我的代码中,在创建 TIdIMAP4 连接之前,我设置了一大堆 SASL 机制,希望按照规定的“最好到最差”顺序,如下所示: IMAP.SASLMechanisms.Add.SASL := mIdS
在 Kubernetes 中,假设我们有 3 个 pod,它们物理上托管在节点 X、Y 和 Z 上。当我使用“kubectl expose”将它们公开为服务时,它们都是集群中的节点(除了 X、Y 和
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我知道进程间通信 (ipc) 有几种方法,例如: 文件 信号 socket 消息队列 管道 命名管道 信号量 共享内存 消息传递 内存映射文件 但是我无法找到将这些机制相互比较并指出它们在不同环境中的
当我尝试连接到 teradata 时,出现了TD2 机制不支持单点登录 错误。 在 C# 中,我遇到了类似的问题,我通过添加 connectionStringBuilder.Authetication
我有一个带有 JSON API 的简单 Javascript 应用程序。目前它在客户端运行,但我想将它从客户端移动到服务器。我习惯于学习新平台,但在这种情况下,我的时间非常有限 - 所以我需要找到绝对
我想了解事件绑定(bind)/解除绑定(bind)在浏览器中是如何工作的。具体来说,如果我删除一个已经绑定(bind)了事件的元素,例如使用 jQuery:$("#anElement").remove
我不是在寻找具体答案,只是一个想法或提示。我有以下问题: Android 应用程序是 Web 服务的客户端。它有一个线程,通过 http 协议(protocol)发送事件(带有请求 ID 的 XML
我正在研究 FreeBSD TCP/IP 栈。似乎有 2 种 syn flood 机制,syncookies 和 syncache。我的问题是关于 syncookies,它是从头开始还是在 SYN 队
我是一名优秀的程序员,十分优秀!