On Fri, Mar 26, 2010 at 10:46 AM, Rafael Cunha de Almeida <almeidaraf@gmail.com> wrote:
Hello,

During a talk with a friend I came up with two programs, one written in
C and another in haskell.

Haskell
       main :: IO ()
       main = print $ rangeI 0 0

       rangeK :: Int -> Int -> Int -> Int -> Int
       rangeK i j k acc
           | k < 1000 =
               if i * i + j * j + k * k `mod` 7 == 0
               then rangeK i j (k+1) (acc+1)
               else rangeK i j (k+1) acc
           | otherwise = acc

       rangeJ :: Int -> Int -> Int -> Int
       rangeJ i j acc
           | j < 1000 = rangeJ i (j+1) (acc + rangeK i j 0 0)
           | otherwise = acc

       rangeI :: Int -> Int -> Int
       rangeI i acc
           | i < 1000 = rangeI (i+1) (acc + (rangeJ i 0 0))
           | otherwise = acc

You might try using bang patterns.  It's possible that GHC didn't detect the strictness.  Only way I know to check is to look at the core.  Use something like ghc-core from Hackage to view it.  Ideally the core is using Int# instead of Int and avoiding lots of boxing.
 


I compiled the haskell code with ghc -O3 and I compiled the C code with
gcc -O3. The execution time of the programs were dramatically different.
You can test it yourselves, but here is the time I've got in my system:

You might try -Odph .
 
See http://book.realworldhaskell.org/read/profiling-and-optimization.html for more ideas.

Jason