C++ c++;用于存储数百万int16的数据结构
下午好。 我有以下情况:有三组数据,每组是一个二维表,其中大约有5000万个字段。(~6000行和~8000列)。 这些数据存储在二进制文件中 语言-c++ 我只需要显示这些数据。 但我在尝试阅读时卡住了。(使用了std::vector,但等待时间太长) 读取/存储如此数量的数据的最佳方式是什么?(标准::向量、简单指针、特殊库)C++ c++;用于存储数百万int16的数据结构,c++,data-structures,C++,Data Structures,下午好。 我有以下情况:有三组数据,每组是一个二维表,其中大约有5000万个字段。(~6000行和~8000列)。 这些数据存储在二进制文件中 语言-c++ 我只需要显示这些数据。 但我在尝试阅读时卡住了。(使用了std::vector,但等待时间太长) 读取/存储如此数量的数据的最佳方式是什么?(标准::向量、简单指针、特殊库) 可能是文章、书籍的链接,或者仅仅是个人体验?如果您不需要一次使用所有这些数据,您可以使用内存映射文件技术读取数据,因为它是一个巨大的数组。一般来说,操作系统/文件系统
可能是文章、书籍的链接,或者仅仅是个人体验?如果您不需要一次使用所有这些数据,您可以使用内存映射文件技术读取数据,因为它是一个巨大的数组。一般来说,操作系统/文件系统缓存对于大多数应用程序来说都能很好地工作,但肯定是YMMV。没有理由不在ifstream/ofstream上使用普通的旧读写。对于BigArray b(6000、8000),下面的代码不需要很长时间
#包括
#包括
#包括
#包括
类BigArray{
公众:
BigArray(intr,intc):行(r),列(c){
数据=(int*)malloc(行*cols*sizeof(int));
if(NULL==数据){
可以在一次调用中释放二进制块吗?您可以对文件进行内存映射,然后将其直接用作数组数组。
#include <fstream>
#include <iostream>
#include <string>
#include <stdlib.h>
class BigArray {
public:
BigArray( int r, int c ) : rows(r), cols(c){
data = (int*)malloc(rows*cols*sizeof(int));
if( NULL == data ){
std::cout << "ERROR\n";
}
}
virtual ~BigArray(){ free( data ); }
void fill( int n ){
int v = 0;
int * intptr = data;
for( int irow = 0; irow < rows; irow++ ){
for( int icol = 0; icol < cols; icol++ ){
*intptr++ = v++;
v %= n;
}
}
}
void readFromFile( std::string path ){
std::ifstream inf( path.c_str(), std::ifstream::binary );
inf.read( (char*)data, rows*cols*sizeof(*data) );
inf.close();
}
void writeToFile( std::string path ){
std::ofstream outf( path.c_str(), std::ifstream::binary );
outf.write( (char*)data, rows*cols*sizeof(*data) );
outf.close();
}
private:
int rows;
int cols;
int* data;
};