环境:windows10、vs2019、release_x86、c++14、cpu(i5-9400 2.9GHz 6核)
//#include "3rdpart/tcmalloc.h" // 引用该头文件或强制符号引用添加__tcmalloc,即可自动替换代码中malloc和new
void new_test()
{
constexpr int loop = 1000*1000*10;
constexpr int len = 20;
long t = 0;
t = clock();
for (int i = 0; i < loop; ++i) {
char* a[len] = { nullptr };
for (int j = 0; j < len; ++j) {
a[j] = (char*)malloc(1500);
}
for (int j = 0; j < len; ++j) {
free(a[j]);
}
}
cout << "malloc var 1500=" << clock() - t << "ms" << endl; // malloc=11250ms tcmalloc=2039ms
t = clock();
for (int i = 0; i < loop; ++i) {
char* a[len] = { nullptr };
for (int j = 0; j < len; ++j) {
a[j] = new char[1500];
}
for (int j = 0; j < len; ++j) {
delete[] a[j];
}
}
cout << "new var 1500=" << clock() - t << "ms" << endl; // new=11507ms tcmalloc=2191ms
t = clock();
for (int i = 0; i < loop; ++i) {
char* a[len] = { nullptr };
for (int j = 0; j < len; ++j) {
a[j] = (char*)tc_malloc(1500);
}
for (int j = 0; j < len; ++j) {
tc_free(a[j]);
}
}
cout << "tc_malloc var 1500=" << clock() - t << "ms" << endl; // 1882ms
getchar();
}
结果上看单线程tcmalloc提升效率5倍左右。cpu均占用17%。
开6线程测试效率tcmalloc提升10+倍左右。