I'm testing for std::forward_list sort performance, generate 1000000 random numbers and insert to std::forward_list, sort for 5 times
#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;
}
}
Compile this code with g++ test2.cpp -o test2 -O2
and run, get output
0.994629 s
3.01309 s
2.98853 s
2.99701 s
3.01637 s
If use tcmalloc, compile this code with g++ test2.cpp /usr/local/lib/libtcmalloc_minimal.a -o test2 -O2
and run, get output
0.551351 s
0.550282 s
0.590626 s
0.613431 s
0.559123 s
If delete code delete list;
, compile with g++ test2.cpp -o test2 -O2
and run, get output
0.893076 s
0.952251 s
0.95971 s
0.931195 s
0.922877 s
So I think the tcmalloc may cause some performance problems?