Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to write large STL vector to file using STL

Tags:

c++

stl

I have a large vector (10^9 elements) of chars, and I was wondering what is the fastest way to write such vector to a file. So far I've been using next code:

vector<char> vs;
// ... Fill vector with data
ofstream outfile("nanocube.txt", ios::out | ios::binary);
ostream_iterator<char> oi(outfile, '\0');
copy(vs.begin(), vs.end(), oi);

For this code it takes approximately two minutes to write all data to file. The actual question is: "Can I make it faster using STL and how"?

like image 861
ljubak Avatar asked Nov 07 '09 13:11

ljubak


2 Answers

With such a large amount of data to be written (~1GB), you should write to the output stream directly, rather than using an output iterator. Since the data in a vector is stored contiguously, this will work and should be much faster.

ofstream outfile("nanocube.txt", ios::out | ios::binary);
outfile.write(&vs[0], vs.size());
like image 155
Charles Salvia Avatar answered Sep 20 '22 20:09

Charles Salvia


There is a slight conceptual error with your second argument to ostream_iterator's constructor. It should be NULL pointer, if you don't want a delimiter (although, luckily for you, this will be treated as such implicitly), or the second argument should be omitted.

However, this means that after writing each character, the code needs to check for the pointer designating the delimiter (which might be somewhat inefficient).

I think, if you want to go with iterators, perhaps you could try ostreambuf_iterator.

Other options might include using the write() method (if it can handle output this large, or perhaps output it in chunks), and perhaps OS-specific output functions.

like image 26
UncleBens Avatar answered Sep 18 '22 20:09

UncleBens