我目前的解析器如下 - 读取~10MB CSV到STL向量需要〜30秒,这对我的喜好来说太慢了,因为我已经有超过100MB的需要在每次读取程序运行。任何人都可以给一些建议如何提高性能?事实上,在普通C中它会更快吗?CSV分析器的性能瓶颈
int main() {
std::vector<double> data;
std::ifstream infile("data.csv");
infile >> data;
std::cin.get();
return 0;
}
std::istream& operator >> (std::istream& ins, std::vector<double>& data)
{
data.clear();
// Reserve data vector
std::string line, field;
std::getline(ins, line);
std::stringstream ssl(line), ssf;
std::size_t rows = 1, cols = 0;
while (std::getline(ssl, field, ',')) cols++;
while (std::getline(ins, line)) rows++;
std::cout << rows << " x " << cols << "\n";
ins.clear(); // clear bad state after eof
ins.seekg(0);
data.reserve(rows*cols);
// Populate data
double f = 0.0;
while (std::getline(ins, line)) {
ssl.str(line);
ssl.clear();
while (std::getline(ssl, field, ',')) {
ssf.str(field);
ssf.clear();
ssf >> f;
data.push_back(f);
}
}
return ins;
}
注:我也有openMP在我的处置,和内容将最终用于与CUDA的GPGPU计算。
你尝试剖析这看到瓶颈在哪里?您还使用了哪种平台和编译器,以及哪些优化设置? – 2013-04-29 22:14:03
尝试不使用std :: vector并使用预分配内存的内存结构(例如数组) –
2013-04-29 22:44:22
@PaulR - 非常感谢。我在MSVC 2010和切换到发布版本比调试快得多 - 我认为你不能在调试版本中使用编译器优化? – mchen 2013-04-29 22:49:19