on master help sign in
Fix pidigits benchmark
commitbccb453a0d2b19b9a519ca7eed1843d47a045dd1 authorPaul Koerbitz <p.koerbitz@smartreporting.de> dateSun 3 Jan 2016, 12:48 (11 years ago) parentec4eab3
1 file changed+10−10
benchmarks/pidigits/pidigits.idr+10−10
benchmarks/pidigits/pidigits.idr+10−10
@@ -1,18 +1,19 @@
11import System
2+import Data.Vect
23
34{- Toy program that outputs the n first digits of Pi.
45
5− Inspired from http://www.haskell.org/haskellwiki/Shootout/Pidigits.
6+ Inspired from http://www.haskell.org/haskellwiki/Shootout/Pidigits.
67 The original ns and str lazy lists have been replaced by strict functions.
78
8− Memory usage seems to be excessive. One of the branches of str is tail recursive, and
9+ Memory usage seems to be excessive. One of the branches of str is tail recursive, and
910 the other one only needs to cons an extra Integer.
1011
11− For reference, the Haskell version runs in 0m0.230s when printing to /dev/null.
12+ For reference, the Haskell version runs in 0m0.230s when printing to /dev/null.
1213 It almost runs in constant space.
1314-}
1415
15−data F = mkF Integer Integer Integer
16+data F = MkF Integer Integer Integer
1617
1718-- Prints the list of digits by groups of 10
1819loop : Nat -> Nat -> List Integer -> IO()
@@ -23,19 +24,19 @@ loop (S k) k' (x::xs) = do putStr (show x)
2324 loop k (S k') xs
2425
2526fn : Integer -> F
26−fn k = mkF k (4*k+2) (2*k+1)
27+fn k = MkF k (4*k+2) (2*k+1)
2728
2829flr : Integer -> F -> Integer
29−flr x (mkF q r t) = (q*x + r) `div` t
30+flr x (MkF q r t) = (q*x + r) `div` t
3031
3132comp : F -> F -> F
32−comp (mkF q r t) (mkF u v x) = mkF (q*u) (q*v+r*x) (t*x)
33+comp (MkF q r t) (MkF u v x) = MkF (q*u) (q*v+r*x) (t*x)
3334
3435-- Returns the list of digits of pi. Memory hungry.
3536str : F -> Integer -> Nat -> List Integer
3637str _ _ Z = Nil
3738str z k (S n) = if(y == flr 4 z)
38− then y :: str (comp (mkF 10 (-10*y) 1) z ) k n
39+ then y :: str (comp (MkF 10 (-10*y) 1) z ) k n
3940 else str (comp z (fn k)) (k+1) (S n)
4041 where y = flr 3 z
4142
@@ -43,10 +44,9 @@ pidigit : IO()
4344pidigit = do
4445 [_,a] <- getArgs
4546 let n = fromIntegerNat (the Integer (cast a))
46− let l = str (mkF 1 0 1) 1 n
47+ let l = str (MkF 1 0 1) 1 n
4748 loop 10 0 l
4849 return ()
4950
5051main : IO ()
5152main = pidigit
52−