[Haskell-cafe] Difference in Runtime but no explanation

Johann Höchtl johann.hoechtl at gmail.com
Tue Dec 15 12:52:20 EST 2009


Hello,

I'm still to Haskell, and after I read through
http://users.aber.ac.uk/afc/stricthaskell.html#seq

I thought, that these tow fragments after term rewriting are really
the same:

myLength :: [a] -> Integer
myLength xs = len xs 0
    where len [] l = l
          len (x:xs) l = l `seq` len xs (l+1)

main = print $ myLength [1..10000000]


-- vs.

myLength :: [a] -> Integer
myLength xs = len xs 0
where len [] l = l
      len (x:xs) l = len xs $! (l+1)

      main = print $ myLength [1..10000000]

main = print $ myLength [1..10000000]

But the first expression evaluates more then twice as fast as the
second one. Tested on GHC 6.10.4 and Windows XP, dual core (for what
it's worth)

It's on http://moonpatio.com/fastcgi/hpaste.fcgi/view?id=5321#a5321
btw.

I can't see the difference, especially as $! is expressed in terms of
seq


More information about the Haskell-Cafe mailing list