[Haskell-cafe] Timing difference

Aai bradypus at xs4all.nl
Thu Dec 25 03:45:33 EST 2008


Programming for Rosetta Codes task 
http://www.rosettacode.org/wiki/Probabilistic_Choice I observed a
remarkable (that's to say: for me) difference in timing using lambda
function in the one case and point free in the other. Timing was
measured in GHCi. Compiled there's no difference!

Using lambda
============

...

labels = ["aleph", "beth", "gimel", "daleth", "he", "waw",
          "zayin", "heth" ]

piv n = take n . flip (++)  (repeat ' ')

main = do
  g <- newStdGen
  let rs :: [Float]
      rs = take 1000000 $ randomRs(0,1) g
      ps, sps :: [Float]
      ps = ap (++) (return. (-) 1 .sum) $ map recip [5.0..11.0]
      sps = scanl1 (+) ps
      ix = map ((/1000000.0).fromIntegral.length). group.sort
           $ map (\x -> fromJust $ findIndex not $ map (x>) sps) rs
  mapM_ (\(l,s,c)-> putStrLn $ (piv 6 l) ++ " " ++ (piv 12 $ show $ s)
++ " "
          ++ ((piv 12 $ show $ c) )) $ zip3 labels ps ix



Using point free
================

replace

 $ map (\x -> fromJust $ findIndex not $ map (x>) sps) rs

by

 $ map (fromJust. findIndex not. flip map sps. (>)) rs

lambda:
=======
*Main> main
...
(18.24 secs, 2524489600 bytes)

point free:
===========
*Main> main
...
(12.15 secs, 2470893260 bytes)


Should I program using point free as much as possible? :-)

Thanks

=@@i




More information about the Haskell-Cafe mailing list