ekmett / rounded

MPFR bindings for Haskell
http://hackage.haskell.org/package/rounded
BSD 3-Clause "New" or "Revised" License
33 stars 13 forks source link

The Show instance is very slow #22

Closed redneb closed 6 years ago

redneb commented 9 years ago

Currently the Show instance uses showFloat from Numeric from base. Unfortunately, that function is rather slow when it comes to numbers with a large number of digits. For example, calculating the first 100,000 decimal digits of π (hence 100,001 significant decimal digits or 332197 significant binary digits) with the following:

length $ show (pi :: Rounded TowardZero 332197)

takes 6 seconds to complete in my system. On the other hand, if I use the Show instance of Integer to find the same digits, e.g. with

length $ show (truncate (pi * 10 ^ 100000 :: Rounded TowardZero 332197) :: Integer)

takes only 0.15 seconds to complete in the same system.

redneb commented 9 years ago

Just to clarify, I don't think we can improve the performance of Show (Rounded r p) by improving showFloat from base. showFloat is too general. I think an ad-hoc instance for Rounded is needed, just like how there is an ad-hoc instance for Show Integer. The latter is defined in GHC.Show and is super-optimized, so maybe it's a good idea to use it somehow.

michalkonecny commented 9 years ago

hmpfr has a Show instance based on mpfr_get_str and it is much faster than rounded's Show instance. (See Conversion.hs.) This binding was written by Aleš Bizjak long time ago. I do not know how hard it is to make a similar binding in rounded.

ekmett commented 9 years ago

That definitely seems worth doing and doesn't look like a difficult port.

claudeha commented 6 years ago

fixed by https://github.com/ekmett/rounded/pull/27 merge