c++ - Reading text file with floating point numbers faster and storing in vector of floats -
i have c++ code written in visual studio 2010, reads text file ( contains tens of thousands of floating point numbers separated space).code reads text file contents , store vector of floating points.my problem , code taking alot of time read , copy vector.is there faster way this.some thing can done in visual studio c++ ( using boost libraries or mmap )
vector<float> replaybuffer; ifstream in; in.open("filename.txt"); if(in.is_open()) { in.setf(ios::fixed); in.precision(3); in.seekg(0,ios::end); filesizes = in.tellg(); in.seekg(0,ios::beg); while(!in.eof()) { for(float f;in>>f;) replaybuffer.push_back(f); } in.close(); }
if files big, consider memory mapped files : boost offer excellent library manipulate them cross platform (you mentioned mmap posix-unix command, , looks developing on windows)
also, consider reserving space in vector avoid dynamic reallocations replaybuffer.reserve(expected_final_size);
note:
- do not use !in.eof() check if finished reading file, it bad practice.
- if dont need
filesizes
, not compute it.
Comments
Post a Comment