From C or C++, I want to read a file of doubles that are in binary format as fast as possible.
Files are small, around 100KB usually (200 KB tops). I want to be able to:
- Read the file of doubles.
- Convert/Store them in a vector of doubles
- Iterate through the vector.
And do these under 2 ms. on this system if possible. Currently it's in around 4-6 milliseconds.
Threads that helped but didn't solve the problem:
Link 2 --> This didn't even compile.
Link 3 --> This didn't work for doubles.
Link 4 --> Doing this.
Here are my file parsers:
"C" style of reading:
void OfflineAnalyser::readNParseData(const char* filePath, vector<double> *&data){
// Temporary Variables
FILE* pFile;
long fileSize;
double *fileBuffer;
size_t sizeOfBuffer;
size_t result;
// Open File
pFile = fopen(filePath, "rb");
if (pFile == NULL){
cout << "File: " << filePath << " does not exist" << endl;
}
// Check whether the parameter is already full
if (!data){
// Reset the output
data->clear();
data = 0;
}
// Obtain file size:
fseek(pFile, 0, SEEK_END);
fileSize = ftell(pFile);
rewind(pFile);
// allocate memory to contain the whole file:
fileBuffer = (double*)malloc(fileSize);
if (fileBuffer == NULL) { fputs("Memory error", stderr); exit(2); }
// copy the file into the buffer:
result = fread(fileBuffer, 1, fileSize, pFile);
if (result != fileSize) {
fputs("Reading error", stderr);
system("pause");
exit(3);
}
// the whole file is now loaded in the memory buffer.
sizeOfBuffer = result / sizeof(double);
// Now convert the double array into vector
data = new vector<double>(fileBuffer, fileBuffer + sizeOfBuffer);
free(fileBuffer);
// terminate
fclose(pFile);
}
Method 2: C++ Style
void OfflineAnalyser::readNParseData2(const char* filePath, vector<double> *&data){
ifstream ifs(filePath, ios::in | ios::binary);
// If this is a valid file
if (ifs) {
// Temporary Variables
std::streampos fileSize;
double *fileBuffer;
size_t sizeOfBuffer;
// Check whether the parameter is already full
if (!data){
// Reset the output
data->clear();
data = 0;
}
// Get the size of the file
ifs.seekg(0, std::ios::end);
fileSize = ifs.tellg();
ifs.seekg(0, std::ios::beg);
sizeOfBuffer = fileSize / sizeof(double);
fileBuffer = new double[sizeOfBuffer];
ifs.read(reinterpret_cast<char*>(fileBuffer), fileSize);
// Now convert the double array into vector
data = new vector<double>(fileBuffer, fileBuffer + sizeOfBuffer);
free(fileBuffer);
}
}
Any suggestions to this code is appreciated. Feel free to type in a code of yourself. I'd be happy if I could see a std::copy for doubles or istream_iterator solutions.
Thanks in advance.