CSV解析器的性能瓶颈
Performance bottleneck with CSV parser
我当前的解析器如下-读取~10MB的CSV到STL向量需要~30s,这对我来说太慢了,因为我有超过100MB的需要在每次程序运行时读取。谁能给一些关于如何提高性能的建议?事实上,用C语言会更快吗?
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在我的处置,内容最终将用于GPGPU计算与CUDA。
只需读取一次文件就可以节省一半的时间。
虽然调整vector的大小是有益的,但它永远不会主导运行时,因为I/O总是会慢一些。
另一个可能的优化是在没有字符串流的情况下读取。比如(未经测试)
int c = 0;
while (ins >> f) {
data.push_back(f);
if (++c < cols) {
char comma;
ins >> comma; // skip comma
} else {
c = 0; // end of line, start next line
}
}
如果您可以省略,
并仅用空格分隔值,则可以是偶数
while (ins >> f)
data.push_back(f);
或
std::copy(std::istream_iterator<double>(ins), std::istream_iterator<double>(),
std::back_inserter(data));
在我的机器上,您的保留代码大约需要1.1秒,您的填充代码需要8.5秒。
ios添加std:::: sync_with_stdio(假);对我的编译器没有影响。
下面的C代码耗时2.3秒。int i = 0;
int j = 0;
while( true ) {
float x;
j = fscanf( file, "%f", & x );
if( j == EOF ) break;
data[i++] = x;
// skip ',' or 'n'
int ch = getc(file);
}
尝试呼叫
std::ios::sync_with_stdio(false);
在程序开始处。这禁用了cin
/cout
和scanf
/printf
之间的同步(据说相当慢)(我自己从未尝试过,但经常看到建议,例如这里)。注意,如果你这样做,你不能在你的程序中混合c++风格和C风格的IO。
(另外,Olaf Dietsche关于只读取文件一次的说法是完全正确的。)
显然,文件IO是一个坏主意,只需将整个文件映射到内存中,访问CSV文件作为一个连续的vm块,这只会引起几个系统调用
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- OpenMP阵列性能较差
- 递归列出所有目录中的C++与Python与Ruby的性能
- 正在将csv文件读取为双精度矢量
- 大小相等但成员数量不同的结构之间的性能差异
- 为什么constexpr的性能比正常表达式差
- 在类中使用随机生成器时出现性能问题
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 如何选择在 csv 文件中输出的位置
- 海湾合作委员会 ARM 性能下降
- GCC 和 Clang 代码性能的巨大差异
- 在容量内调整矢量大小时的性能影响
- CPU 瓶颈;处理具有许多非静态对象的 3D 场景渲染的简单方法
- 了解算法的性能差异(如果以不同的编程语言实现)
- 在C 中读取大型CSV文件性能问题
- Linux 与 Windows,C++读取 CSV 文件的运行时性能差异
- Python Pandas 的性能与文本 CSV 数据导入C++解决方案
- 性能瓶颈在_L_unlock_16
- CSV解析器的性能瓶颈
- 性能瓶颈——使用Visual Studio