pi benchmark on ldc and dmd

bearophile bearophileHUGS at lycos.com
Tue Aug 2 13:35:23 PDT 2011


Adam D. Ruppe:

> Here's the program. It's based on one of the Python ones.

The D code is about 2.8 times slower than the Haskell version, and it has a bug, shown here:

import std.stdio, std.bigint;
void main() {
    int x = 100;
    writefln("%010d", x);
    BigInt bx = x;
    writefln("%010d", bx);
}

Output:
0000000100
100

----------------------------

The Haskell code I've used:

-- Compile with:  ghc --make -O3 -XBangPatterns -rtsopts pidigits_hs.hs
import System

pidgits n = 0 % (0 # (1,0,1)) where
 i%ds
  | i >= n = []
  | True = (concat h ++ "\t:" ++ show j ++ "\n") ++ j%t
  where k = i+10; j = min n k
        (h,t) | k > n = (take (n`mod`10) ds ++ replicate (k-n) " ",[])
              | True = splitAt 10 ds
 j # s | n>a || r+n>=d = k # t
     | True = show q : k # (n*10,(a-(q*d))*10,d)
  where k = j+1; t@(n,a,d)=k&s; (q,r)=(n*3+a)`divMod`d
 j&(n,a,d) = (n*j,(a+n*2)*y,d*y) where y=(j*2+1)

main = putStr.pidgits.read.head =<< getArgs

Bye,
bearophile


More information about the Digitalmars-d mailing list