[Haskell-beginners] force strict expression evaluation

Ovidiu Deac ovidiudeac at gmail.com
Tue Aug 2 17:58:59 CEST 2011


I'm trying to do some performance evaluation and I'm stuck with the
fact that Haskell's lazy evaluation - which make my sort extremely
fast :)

I read this page:
http://www.haskell.org/haskellwiki/Performance/Strictness but I didn't
get it so I'm asking here: How do I make the function 'measure' to
actually force the evaluation of (f p)?

Thanks,
ovidiu

See the code below:
------------------------------------
module Main where
import Prelude
import Data.List
import Data.Time.Clock
import System.Random

quickSort [] = []
quickSort (x:xs) = (quickSort small) ++ [x] ++ (quickSort big)
        where
            small = [p | p <- xs, p <= x]
            big = [p | p <- xs, p > x]

randomlist :: Int -> StdGen -> [Int]
randomlist n = take n . unfoldr (Just . random)

len = 10 ^ 10

measure f p = do
    t1 <- getCurrentTime
    let sorted = f p
    t2 <- getCurrentTime
    let diff = diffUTCTime t2 t1
    return diff

main = do
    seed  <- newStdGen
    let rs = randomlist len seed

    putStrLn $ "Sorting " ++ (show len) ++ " elements..."

    t <- measure quickSort rs

    putStrLn $ "Time elapsed: " ++ (show t)



More information about the Beginners mailing list