[Haskell-cafe] Re: newbie optimization question

Peter Hercek peter at syncad.com
Sun Oct 28 18:34:29 EDT 2007


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)]

main = print perfect

<---cut--->
and here is the C++ version:
<---cut--->

#include <iostream>
using namespace std;

int main() {
   for (int i = 1; i <= 10000; i++) {
     int sum = 0;
     for (int j = 1; j < i; j++)
       if (i % j == 0)
         sum += j;
     if (sum == i)
       cout << i << " ";
   }
   return 0;
}

<---cut--->

OS winxp 64 bit
ghc v. 6.6.1 (optins -O2)
MS cl.exe version 13.10.3077 (options /G7 /MD)

Peter.



More information about the Haskell-Cafe mailing list