I'm trying to read doubles from a relatively small binary file. This currently reads a 100 KB file in about 6 milliseconds in my system. I would like to reduce that if possible.
void readNParseData(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 != 0){
// 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);
}
}
As you can see there is a redundant copy of a double *
array to a vector. Perhaps reading to the vector directly might speed it up, but I don't know how.