federico.squartini:
Of course I know that the list version is very unfair, but I wanted to see what was the trade off between elegance and speed. Regarding whether low level programming makes sense or not, I was just curious to see what are the limits of Haskell. Moreover there is not much literature on high performance Haskell programming (tricks like unsafeWrite), at least organized in a systematic and concise way. My original problem was writing a fast library for simple matrix computations (i.e. multiplication and inversion for small dense matrices). I have not been able to make GSLHaskell work with Lapack so far. :( Anyway here are the new versions and timings, I increased the number of times the vector is reversed, I also compiled everything with -O2.
Probably a good idea to use techniques from Data.ByteString (ie. use strict Ptr loops, and Foreign arrays), or techniques from the shootout, if you're chasing C speed. Good examples are: http://shootout.alioth.debian.org/gp4/benchmark.php?test=nsievebits&lang=ghc... (mutable bit arrays) http://shootout.alioth.debian.org/gp4/benchmark.php?test=nsieve&lang=ghc&id=... (mutable byte (foreign) arrays) http://shootout.alioth.debian.org/gp4/benchmark.php?test=spectralnorm&lang=g... (more mutable arrays) When I really really care about speed, I use Foreign.Marshal.Array Data.ByteString and apply ! patterns liberally, checking the Core output for inner loops. -O2 -optc-O2 -optc-march=pentium4 often helps. 1-4x C is around what you can best hope for. 10x says "still room for improvement" in my experience. -- Don