[Haskell-cafe] how to make haskell faster than python at finding primes?

Donald Bruce Stewart dons at cse.unsw.edu.au
Mon Aug 6 04:03:15 EDT 2007


alex:
> This implementation of calculating 10000 primes (compiled with GHC -O2) 
> is 25% slower than the naive python implementation.  Shouldn't it be 
> faster?  Am I doing something obviously stupid?

Why not just:

    primes         = sieve [2..]

    sieve (p : xs) = p : sieve [x | x <- xs, x `mod` p > 0]

    main           = print (take 1000 primes)

That's super naive, and seems to be around 5x faster than the code you were
trying. (So make sure you're doing the same thing as the python version)
    
-- Don 


More information about the Haskell-Cafe mailing list