[Haskell-cafe] Re: newbie optimization question

Don Stewart dons at galois.com
Sun Oct 28 23:04:43 EDT 2007


peter:
> Don Stewart wrote:
> >>C++ version times: 1.109; 1.125; 1.125
> >>Int32 cpu times: 1.359; 1.359; 1.375
> >>Int64 cpu times: 11.688; 11.719; 11.766
> >>Integer cpu times: 9.719; 9.703; 9.703
> >>
> >>Great result from ghc.
> >
> >What Haskell program were you using for this test? The original
> >naive/high level implementation?
> >
> >-- Don
> 
> Here it is (I played only with the types for divisors and perfect;
>  bottom is there from my previous tests, but it should not matter):
> <---cut--->
> 
> module Main (bottom, divisors, perfect, main) where
> import Data.Int
> 
> bottom = error "_|_"
> 
> divisors :: Int -> [Int]
> divisors i = [j | j<-[1..i-1], i `mod` j == 0]
> 
> perfect :: [Int]
> perfect = [i | i<-[1..10000], i == sum (divisors i)]
> 

This should be a little faster , as sum will fuse,

    perfect :: [Int]
    perfect = [i | i<-[1..10000], i == sum' (divisors i)]
        where sum' = foldr (+) 0


More information about the Haskell-Cafe mailing list