我将 Java GC 测试程序移植到 C++(见下面的代码)和 Python。Java 和 Python 的性能比 C++ 好得多,我认为这是由于new
每次都必须执行所有调用来创建字符串。我试过使用 Boost fast_pool_allocator
,但实际上性能从 700ms 恶化到 1200ms。我使用分配器是错误的,还是我应该做的其他事情?
编辑:用g++ -O3 -march=native --std=c++11 garbage.cpp -lboost_system
. g++ 是 4.8.1 版本,Python 的一次迭代大约需要 300 毫秒,Java 大约需要 50 毫秒。std::allocator
大约 700 毫秒,大约 1200 毫秒boost::fast_pool_allocator
。
#include <string>
#include <vector>
#include <chrono>
#include <list>
#include <iostream>
#include <boost/pool/pool_alloc.hpp>
#include <memory>
//#include <gc/gc_allocator.h>
using namespace std;
#include <sstream>
typedef boost::fast_pool_allocator<char> c_allocator;
//typedef std::allocator<char> c_allocator;
typedef basic_string<char, char_traits<char>, c_allocator> pool_string;
namespace patch {
template <typename T> pool_string to_string(const T& in) {
std::basic_stringstream<char, char_traits<char>, c_allocator> stm;
stm << in;
return stm.str();
}
}
#include "mytime.hpp"
class Garbage {
public:
vector<pool_string> outer;
vector<pool_string> old;
const int nThreads = 1;
//static auto time = chrono::high_resolution_clock();
void go() {
// outer.resize(1000000);
//old.reserve(1000000);
auto tt = mytime::msecs();
for (int i = 0; i < 10; ++i) {
if (i % 100 == 0) {
cout << "DOING AN OLD" << endl;
doOld();
tt = mytime::msecs();
}
for (int j = 0; j < 1000000/nThreads; ++j)
outer.push_back(patch::to_string(j));
outer.clear();
auto t = mytime::msecs();
cout << (t - tt) << endl;
tt = t;
}
}
void doOld() {
old.clear();
for (int i = 0; i < 1000000/nThreads; ++i)
old.push_back(patch::to_string(i));
}
};
int main() {
Garbage().go();
}