最近在测试tcmalloc性能的时候发现了一个现象!!!!就是new似乎很消耗性能!!直接上代码!
#include "time.h"
#include <gperftools/tcmalloc.h>
using namespace std;
#define MAX_SIZE 500000
//MsgDuplex _msg_list;
struct MsgToPut
{
UINT _wr_ptr;
UINT _rd_ptr;
UINT _length;
CHAR _base[1024];
};
int main()
{
long long int start = get_os_system_time();
MessageBlock* mbs[MAX_SIZE];
for(int i=0;i<MAX_SIZE;i++)
{
mbs[i] = new MessageBlock(1024);
delete mbs[i];
}
long long int end = get_os_system_time();
printf("%d\n",end - start);
MsgToPut* mtp[MAX_SIZE];
for(int i=0;i<MAX_SIZE;i++)
{
mtp[i] = (MsgToPut*)tc_malloc(sizeof(MsgToPut));
tc_delete(mtp[i]);
}
start = get_os_system_time();
printf("%d\n",start - end);
return 0;
}
MessageBlock 在博文http://www.cnblogs.com/archy_yu/archive/2012/09/07/2674909.html中有介绍!!!
我们看下打印数据!
28
10
既然new在执行的时候运行了构造函数,那么我们也做一个reset操作; 并且在每次mtp[i] = (MsgToPut*)tc_malloc(sizeof(MsgToPut));之后执行reset函数,但是效果影响不大,打印数据为:
28
12
new 相比malloc 和 tcmalloc要消耗性能!!!而且很客观!!!