这周我发现了 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/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
通过rubykoans.com,我在about_array_assignment.rb中遇到了这两段代码你怎么知道第一个是非并行赋值,第二个是一个变量的并行赋值?在我看来,除了命名差异之外,代码几乎完全相同。4deftest_non_parallel_assignment5names=["John","Smith"]6assert_equal["John","Smith"],names7end45deftest_parallel_assignment_with_one_variable46first_name,=["John","Smith"]47assert_equal'John
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我在app/helpers/sessions_helper.rb中有一个帮助程序文件,其中包含一个方法my_preference,它返回当前登录用户的首选项。我想在集成测试中访问该方法。例如,这样我就可以在测试中使用getuser_path(my_preference)。在其他帖子中,我读到这可以通过在测试文件中包含requiresessions_helper来实现,但我仍然收到错误NameError:undefinedlocalvariableormethod'my_preference'.我做错了什么?require'test_helper'require'sessions_hel