A way to measure performance

Posted by Andrei Ciobanu on Stack Overflow See other posts from Stack Overflow or by Andrei Ciobanu
Published on 2011-01-11T22:30:56Z Indexed on 2011/01/12 1:54 UTC
Read the original article Hit count: 453

Filed under:

Given Exercise 14 from 99 Haskell Problems:

(*) Duplicate the elements of a list.

Eg.:

*Main> dupli''' [1..10]
[1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10]

I've implemented 4 solutions:

{-- my first attempt --}
dupli :: [a] -> [a]
dupli [] = []
dupli (x:xs) = replicate 2 x ++ dupli xs

{-- using concatMap and replicate --}
dupli' :: [a] -> [a]
dupli' xs = concatMap (replicate 2) xs

{-- usign foldl --}
dupli'' :: [a] -> [a]
dupli'' xs = foldl (\acc x -> acc ++ [x,x]) [] xs

{-- using foldl 2 --}
dupli''' :: [a] -> [a]
dupli''' xs = reverse $ foldl (\acc x -> x:x:acc) [] xs

Still, I don't know how to really measure performance .

So what's the recommended function (from the above list) in terms of performance .

Any suggestions ?

© Stack Overflow or respective owner

Related posts about haskell