be67271
committed
Commits
Comments (0)
Files changed (1)

+26 7README
README
The Show instances for RealFloat types provided in base are very elegant, as they produce the shortest string which 'read' converts back to the original number.
That, however, involves a check after each digit has been determined and arithmetic of usually fairly large Integers, which makes these Show instances rather slow.
For cases where having the conversion fast is more important than having it produce elegant output, this package provides alternative conversions, which avoid the checks and reduce the occurrences of large Integers by just producing a sufficiently long output string.
The speed gain can be substantial if the numbers have exponents of large absolute modulus, but for the more common case of numbers whose exponents have small absolute modulus, the difference is (although still significant for Double) too small in my opinion to seriously consider replacing the Show instances.
Some benchmarks produced with criterion (http://hackage.haskell.org/package/criterion) show a considerable speedup, or example, converting 10,000 numbers to String and calculating the length produced:
The difference is far smaller however if we consider only numbers with exponents of small absolute modulus (numbers between 1e8 and 1e8 here):
Another benchmark, calculating d*sin(d^2) for d = 1, ..., 200000 and writing their string representations to a file, timed 'show' at 4.26s, 'fshow' at 1.08s and 'show' for the newtype wrapper Double7 (rounds to seven significant digits) around Double at 0.55s. A corresponding C programme ran in 0.26s with the default precision and in 0.32s with a precision of 17 digits.