ptmalloc导致的性能问题?

问题描述 投票:0回答:0

我正在测试 std::forward_list 排序性能,生成 1000000 个随机数并插入到 std::forward_list,排序 5 次

#include <forward_list>
#include <chrono>
#include <random>
#include <iostream>
#include <vector>

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> dis(-100000000, 100000000);
    std::vector<int> v;
    for (int i = 0; i < 1000000; ++i) {
        v.push_back(dis(gen));
    }
    std::forward_list<int> *list;
    for (int j = 0; j < 5; ++j) {
        auto list = new std::forward_list<int>();
        for (auto it = v.begin(); it != v.end(); ++it) {
            list->insert_after(list->cbefore_begin(), *it);
        }
        auto start = std::chrono::steady_clock::now();
        list->sort();
        auto end = std::chrono::steady_clock::now();
        std::chrono::duration<double> diff = end - start;
        std::cout << diff.count() << " s\n";
        delete list;
    }
}

g++ test2.cpp -o test2
编译这段代码并运行,得到输出

1.56004 s
3.58784 s
3.64035 s
3.58166 s
3.55821 s

如果使用 tcmalloc,用

g++ test2.cpp /usr/local/lib/libtcmalloc_minimal.a -o test2
编译这段代码并运行,得到输出

1.17486 s
1.27712 s
1.22682 s
1.24105 s
1.17127 s

如果删除代码

delete list;
,用
g++ test2.cpp -o test2
编译并运行,得到输出

1.38157 s
1.38446 s
1.38761 s
1.36721 s
1.39012 s

所以我认为 tcmalloc 可能会导致一些性能问题?

c++ performance malloc tcmalloc
© www.soinside.com 2019 - 2024. All rights reserved.