[Haskell-cafe] Re: another Newbie performance question

apfelmus apfelmus at quantentunnel.de
Sun May 18 03:41:22 EDT 2008

Achim Schneider wrote:
> 1) traverse the list less often. 

Luke Palmer wrote:
> Philip Müller wrote:
>> If someone here finds the time to look at my code and give me some hints,
>> that would really be nice.
> There are probably a few other tricks you could do, but I think I
> identified the main factors.

List concatenation (++) takes time proportional to the length of the 
first list. In other words, every (xs ++) traverses xs again. So, things 
like (x ++ "\n") are expensive. Better write  writeCSV as

   writeCSV = unlines . map (concat . intersperse "," . map show)

Here,  unlines  is a library function which implements your

   (\x -> x ++ "\n") . concat . intersperse "\n"

but slightly more efficiently.

In general, difference lists are better for output (concatenation) than 
Strings and ByteStrings. So, if you want to squeeze even more speed out 
of your program, use those.


More information about the Haskell-Cafe mailing list