天天看點

c++記憶體管理優化之ptmalloc,tcmalloc,jemalloc使用執行個體

​​ptmalloc​​ 是glibc的記憶體配置設定管理

​​tcmalloc​​ 是google的記憶體配置設定管理子產品

​​jemalloc​​ 是BSD的提供的記憶體配置設定管理

寫一段代碼測試一下

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
extern "C"
{
#include "jemalloc.h"
}
#include <map>
#include <string>
using namespace std;
int main()
{
    char *buff = new char[32];
lab_beg:
    clock_t begin = clock();
    int i = 0;
    for(i=0;i<10000000;i++)
    {
        char *buff = (char *)malloc(1024);
    }
    map<int, string> map_container;
    for(int i=0;i<1000000;i++)
    {
        map_container.insert(make_pair(i, "hello world"));
    }
    printf("%d new char[1024] costs:%f s\n", i, (double)(clock()-begin)/CLOCKS_PER_SEC);
    getchar();
    goto lab_beg;
}      
INCLUDE=
LIB_PATH=
LIBS=
FLAGS=-D_Debug -Wl,-Bstatic -DJEMALLOC_NO_RENAME
CXXFALGS=

test:test.cpp
  g++ -g test.cpp -o test -I./

test_je:test.cpp
  g++ -g test.cpp -o test_je -I./ -lgcc_s -L./ -ljemalloc

test_tc:test.cpp
  g++ -g test.cpp -o test_tc -L../../gpertools-2.1/.libs -ltcmalloc

clean:
  rm -f test      

結果好像差别不大,jemalloc的結果不知道為何出不來了。。。