I have a code written in c++ reading a very big data file (10-20 go). I read each line and it is rather long. Is there any way to improve the effiencicy ?
I know that there is some post about this but my problem is not excatly the same...
The file is containing coordinates of N atoms and their velocity at given times.
My code :
void Funct(std::string path, float TimeToRead, int nbLines, float x[], float y[], float z[], float vx[], float vy[], float vz[], std::string names[], int index[])
{
ifstream file(path.c_str());
if (file)
{
/* x,y,z are arrays like float x[nbAtoms] */
while (time != TimetoRead) {
/*I Put the cursor at the given time to read before*/
/*And then read atoms coordinates*/
}
for (int i = 0; i < nbAtoms; i++) {
file >> x[i]; file >> y[i]; /* etc, load all*/
}
}
}
int main()
{
/*Declarations : hidden*/
for (int TimeToRead = 0; TimeToRead<finalTime; TimeToRead++) {
Funct(...);
/*Do some Calculus on the atoms coordinates at one given time */
}
}
Currently i have around 2million lines with 8 or 9 column of number each. The file is a succesion of atom coordinates at one given time.
I have to do calculation on each time step, so i am now calling this function for every time step (around 4000 time steps and there is a large amount of atoms). At the end is very expensive in time.
I have read somewhere that i could save in memory in one row and not read file every time but when the file is 20Go i cannot really save it all in RAM!
What can i do to improve this reading ?
Thanks you very much
Edit1: I am on Linux
Edit2: The file to read contains a line header like :
time= 1
coordinates atom 1
coordinate atom 2
...
...
...
time=2
coordinates atom 1
coordinate atom 2
...
...
...
etc
the while loop is just reading each line since the begining until he finds the t= TimeToRead