Hello

Most of my C++ is self taught so please excuse me

In my code I am writing a large amount of data to file

for(int i=0; i<itr_atoms; i++)
      file_out << i+1 << " 1 " << atoms[i][0] + origin_x << " " << atoms[i][1] + origin_y << " " << atoms[i][2] + origin_z << '\n';

where itr_atoms can be 50,000,000+

As you can guess this is slow

Is there any way i can make this more efficient

Writing binary file would be more efficient, but unreadable with any standard text editor such as Notepad. Note that file_out has to be opened in binary mode.

file_out.write( (char *) atoms, sizeof( atoms));
file_out.write( (char *)&origin_x, sizeof(origin_x));
file_out.write( (char *)&origin_y, sizeof(origin_y));
file_out.write( (char *)&origin_z, sizeof(origin_z));

Then to read it back, just replace write() with read()

Thank you for your quick reply

I will go and learn how to read and write binary files

I have 150 lines of code to change (I have a fun afternoon ahead)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.