[Haskell-cafe] More fun with micro-benchmarks and optimizations. (GHC vs Perl)

Luke Palmer lrpalmer at gmail.com
Wed Jul 23 18:33:20 EDT 2008

On Wed, Jul 23, 2008 at 6:10 PM, Corey O'Connor <coreyoconnor at gmail.com> wrote:
> My straightforward Haskell solution was:
> -------------------------------------------------------------------
> import Text.Regex.Posix
> main = do
>    f_lines <- readFile "test.out" >>= return . lines
>    let duration = foldl add_line 0.0 f_lines
>        add_line sum line =
>            let [[_,m]] = line =~ "([0-9.]+)"
>                framerate = read m
>                delta = 1.0 / framerate
>            in sum + delta
>    putStrLn $ "Duration (sec): " ++ show duration
> -------------------------------------------------------------------

I'm sure the other suggestions have improved performance far more than
this suggestion will, but it's still worth a shot.

I don't think I've _ever_ seen a case where using foldl was a good
idea.   foldl interacts poorly with laziness; use foldr when the
function you are folding is nonstrict, and use foldl' (from
Data.List), a strict version of foldl, when the function you are
folding is strict.  This is, of course, only a rule of thumb.

Changing that foldl to foldl' will probably speed this up a decent amount.


More information about the Haskell-Cafe mailing list