Chinaunix首页 | 论坛 | 博客
  • 博客访问: 349167
  • 博文数量: 46
  • 博客积分: 4936
  • 博客等级: 上校
  • 技术积分: 575
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-25 20:14
文章分类

全部博文(46)

文章存档

2012年(4)

2011年(1)

2010年(23)

2009年(18)

分类: LINUX

2010-01-27 22:19:15

使用Tcmalloc的性能测试结果

今天研究了一下tcmalloc的使用,感觉效果惊人,很是激动,特此写出来以飨读者。关于tcmalloc的介绍,参考文章:TCMalloc:线程缓存的Malloc,是从google官方网站翻译出来的。

Tcmalloc的使用很简单,只需要-ltcmalloc_minimal即可。

测试代码:

#include

#include

#include

#include

#include

#include

 

#define MAX_COUNT 1000*1000

void fun(int i)

{

         char* ptr = (char*)malloc(i);

         free(ptr);

}

 

void* fun_thread(void*)

{

         int i = 0;

         int j = 1;

         while(i++

         {

                   j ++;

                   fun(j);

 

                   if ( j>1024 )

                            j = 1;

         }

}

 

#define MSECOND 1000000

int main()

{

 

         struct timeval tpstart,tpend;

         float timeuse;

        

         gettimeofday(&tpstart,NULL);

 

         pthread_t _deliver_t;

 

         pthread_create(&_deliver_t, NULL, fun_thread, NULL);

         int i = 0;

         int j = 1;

         while(i++

         {

                   j ++;

                   fun(i);

                   if ( j > 1024 )

                            j = 1;

                   //usleep(1);

         }

        

         gettimeofday(&tpend,NULL);

 

         timeuse=MSECOND*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;

         timeuse/=MSECOND;

         printf("Used Time:%f\n", timeuse);

 

         return 0;

}

 

测试结果很是诱人:

[root@localhost test]# g++ 1.c -o 1 -lpthread

[root@localhost test]# ./1

Used Time:5.336594

[root@localhost test]# g++ 1.c -o 1 -lpthread -ltcmalloc_minimal

[root@localhost test]# ./1

Used Time:0.208050

 

提高了几十倍!当然测试的条件不是很完整,但是可以肯定tcmalloc效率提高了很多

阅读(2590) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~