boost智能指针的性能测试
来源:互联网 发布:ubuntu mate 输入法 编辑:程序博客网 时间:2024/05/01 18:51
智能指针为内存管理带来了挺大的便利,不过相比传统指针到底慢了多少,是本文要探讨的。
首先上测试代码
====================================================
#include <iostream>
#include <ctime>
#include <sys/timeb.h>
#include<boost/shared_array.hpp>
using namespace std;
time_t getms()
{
timebtb;
ftime(&tb);
return(int)(tb.time*1000 + tb.millitm);
}
int main()
{
int times =1000000000;
{
int* p = newint[100];
time_toldtime = getms();
for (int i =0; i < times; ++ i)
{
int* a =p;
a[0] =i;
}
time_tnewtime = getms();
cout<< (newtime - oldtime)<< endl;
delete[]p;
}
{
boost::shared_array<int> p(newint[100]);
time_toldtime = getms();
for (int i =0; i < times; ++ i)
{
boost::shared_array<int> a =p; // copyconstructor
a[0] =i; //visit
}
time_tnewtime = getms();
cout<< (newtime - oldtime)<< endl;
}
{
boost::shared_array<int> p(newint[100]);
time_toldtime = getms();
for (int i =0; i < times; ++ i)
{
boost::shared_array<int> a =p; // copyconstructor
}
time_tnewtime = getms();