1. 问题
有一系列的数据文件,以txt的形态存在,具体的格式如下:
123 44 12
124 45 67
23 45 67
...
上述文件中,每行三个int类型的数据,以空格隔开,每行末尾是换行
现在,需要将其读入到内存中,并且存储于向量之中,向量的组织结构如下:
//声明
std::vector<int*> pointVec;
//储存
int* point=new int[3];
point[0]=123;
point[1]=44;
point[2]=12;
pointVec.push_back(point);
个人预测数据量在百万级,因此写文件、读文件成为一个比较重要的过程,毕竟I/O操作是比较消耗时间的。
2. 读取方案
一百万行数据存储在txt文件中大概需要13M,单纯以二进制读取的时间消耗是微秒级别的,一旦中途需要将其拆分为int就会变得很慢。
2.1 混合QT与C++的读取方式
std::vector<int*>* HeatMap::readSegmentInfo(const char* t_file, int* t_header) {
std::vector<int*>* pointVec = new std::vector<int*>;
std::ifstream in;
in.open(t_file, std::ios::in);
if (!in.is_open()) return nullptr;
while (std::getline(in, line)) {
QString tLine = QString::fromLocal8Bit(line.c_str());
QStringList lineList = tLine.split(" ");
if (lineList.count() == 3) {
int* arr = new int[3];
arr[0] = lineList[0].toInt();
arr[1] = lineList[1].toInt();
arr[2] = lineList[2].toInt();
pointVec->push_back(arr);
}
}
in.close();
}
时间:11s左右
2.2 stringstream的读取方式
std::vector<int*>* HeatMap::readSegmentInfo(const char* t_file, int* t_header) {
std::vector<int*>* pointVec = new std::vector<int*>;
std::ifstream in;
in.open(t_file, std::ios::in);
if (!in.is_open()) return nullptr;
std::stringstream buffer;
buffer << in.rdbuf();
while (!buffer.eof()) {
int* arr = new int[3];
buffer >> arr[0];
buffer >> arr[1];
buffer >> arr[2];
pointVec->push_back(arr);
}
in.close();
}
时间:11s左右
2.3 C风格fscanf()的读取方式
std::vector<int*>* HeatMap::readSegmentInfo(const char* t_file, int* t_header) {
std::vector<int*>* pointVec = new std::vector<int*>;
FILE* fp;
char line[1000] = { 0 };
fp = fopen(t_file, "r");
while (!feof(fp)) {
int* arr = new int[3];
fscanf(fp, "%d", &arr[0]);
fscanf(fp, "%d", &arr[1]);
fscanf(fp, "%d", &arr[2]);
pointVec->push_back(arr);
}
fclose(fp);
}
//也可以手动逐个字符的读取,通过空格换行作为中断或者结束标志,
//手动将buffer转化为int最后存储在向量中,这样也能够达到效果,
//时间与直接使用fscanf相差不大。
时间:2~3s [即使已经很快了,但是依旧觉得慢]
3. 思考&相关链接
不知道是多久以前写的了,停止思考😭