The first thing to do is determine where time is going. Formatting and parsing the text are not trivial and can take some time, but you can actually write and read, given the size of the file. Secondly, to determine what the data should be: the fastest solution is almost certainly the mmap (or its Windows equivalent) array to the file directly, and never read or write. This does not however provide a portable representation, and even the compiler can make the data unreadable. (It is unlikely that 32 bits are integers today, but this happened in the past).
In general, if time is going to read and write, you will want to explore using mmap . If it will be formatting and parsing, you will want to conduct a study of the binary format - it can also help in reading and writing if it reduces the resulting files. The simplest binary format, recording values ββusing a standard network standard, requires no more than:
void writeInt( std::ostream& dest, int32_t integer ) { dest.put( (integer >> 24) & 0xFF ); dest.put( (integer >> 16) & 0xFF ); dest.put( (integer >> 8) & 0xFF ); dest.put( (integer ) & 0xFF ); } int32_t readInt( std::istream& source ) { int32_t results = 0; results = source.get() << 24; results |= source.get() << 16; results |= source.get() << 8; results |= source.get(); return results; }
(Some error checking should obviously be added.)
If many of the integers are actually small, you can try some variable-length coding, for example, used in the Google Buffers protocol. If most of your integers are in the range -64 ... 63, this can lead to the file being only a quarter of the size (which again will improve the time it takes to read and write).
source share