快速,简单的CSV解析在C ++ [英] Fast, Simple CSV Parsing in C++

查看:206
本文介绍了快速,简单的CSV解析在C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析一个简单的CSV文件,数据格式如下:

I am trying to parse a simple CSV file, with data in a format such as:

20.5,20.5,20.5,0.794145,4.05286,0.792519,1
20.5,30.5,20.5,0.753669,3.91888,0.749897,1
20.5,40.5,20.5,0.701055,3.80348,0.695326,1

所以,一个非常简单和固定的格式文件。我将这些数据的每一列存储到一个STL向量中。因此,我试图使用标准库保持C ++的方式,我在循环中的实现看起来像:

So, a very simple and fixed format file. I am storing each column of this data into a STL vector. As such I've tried to stay the C++ way using the standard library, and my implementation within a loop looks something like:

string field;
getline(file,line);
stringstream ssline(line);

getline( ssline, field, ',' );
stringstream fs1(field);
fs1 >> cent_x.at(n);

getline( ssline, field, ',' );
stringstream fs2(field);
fs2 >> cent_y.at(n);

getline( ssline, field, ',' );
stringstream fs3(field);
fs3 >> cent_z.at(n);

getline( ssline, field, ',' );
stringstream fs4(field);
fs4 >> u.at(n);

getline( ssline, field, ',' );
stringstream fs5(field);
fs5 >> v.at(n);

getline( ssline, field, ',' );
stringstream fs6(field);
fs6 >> w.at(n);

问题是,这是非常慢的(每个数据文件有超过100万行)似乎对我来说有点不高兴。有更快的方法使用标准库,或者我应该使用stdio函数?在我看来,整个代码块将减少到单个fscanf调用。

The problem is, this is extremely slow (there are over 1 million rows per data file), and seems to me to be a bit inelegant. Is there a faster approach using the standard library, or should I just use stdio functions? It seems to me this entire code block would reduce to a single fscanf call.

提前感谢!

推荐答案

使用7个字符串流,当你可以做到只用一个肯定不帮助wrt。性能。
尝试这样:

Using 7 string streams when you can do it with just one sure doesn't help wrt. performance. Try this instead:

string line;
getline(file, line);

istringstream ss(line);  // note we use istringstream, we don't need the o part of stringstream

char c1, c2, c3, c4, c5;  // to eat the commas

ss >> cent_x.at(n) >> c1 >>
      cent_y.at(n) >> c2 >>
      cent_z.at(n) >> c3 >>
      u.at(n) >> c4 >>
      v.at(n) >> c5 >>
      w.at(n);

如果知道文件中的行数,可以在读取之前调整向量大小,然后请使用 operator [] 而不是在()。这样你避免了边界检查,从而获得一点性能。

If you know the number of lines in the file, you can resize the vectors prior to reading and then use operator[] instead of at(). This way you avoid bounds checking and thus gain a little performance.

这篇关于快速,简单的CSV解析在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆