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 |
---|---|---|---|---|---|---|---|---|
10^1 | 1.00 | 2.25 | 1.27 | 1.10 | 1.81 | 6.04 | 9.98 | 18.64 |
10^2 | 1.00 | 2.21 | 1.33 | 1.09 | 1.91 | 6.07 | 9.91 | 18.62 |
10^3 | 1.00 | 2.45 | 1.80 | 1.41 | 3.01 | 6.68 | 12.16 | 22.06 |
10^4 | 1.00 | 2.33 | 2.25 | 1.38 | 2.47 | 6.24 | 12.06 | 21.12 |
10^5 | 1.00 | 1.33 | 1.80 | 2.76 | 4.46 | 4.18 | 7.98 | 14.48 |
10^6 | 1.00 | 1.12 | 1.87 | 2.27 | 3.85 | 3.52 | 6.86 | 12.76 |
10^7 | 1.00 | 1.21 | 2.27 | 2.97 | 4.75 | 3.79 | 7.90 | 14.05 |
10^8 | 1.00 | 1.27 | 2.78 | 3.07 | 4.74 | 4.00 | 6.72 | 15.08 |
10^9 | 1.23 | 1.00 | 2.70 | 3.85 | 5.11 | 3.49 | 6.08 | 13.73 |
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....
No comments:
Post a Comment