There are 3 solutions to this problem:
- Store numbers in binary in the file. For that one needs to define a good format for storing binary data. That wuold be really the best approach.
- Write or find a good BCD (Binary-coded decimal) library. I know... sounds old. A bit like perl, I guess. But, as long as one doesn't to too complicated math, this will be almost as fast as 1.
- improve read/write of numbers.
I went on a hunt for an efficient itoa, but didn't really find much.
In the end, I wrote the itoa mentioned in my last post. That itoa was fixed width, now I also added variable width.
Here is a table of the relative speeds of various implementations:
|size of number||my itoa||my itoa_fill0||itoa1||ufast_itoa10||new_itoa||sstream||i32toa||sprintf|
It is easy to see what the problem is. sprintf is really slow. sstream is actually surprisingly fast.... atoi next. Then, just reading and writing from files....