Revision as of 06:03, 24 April 2006

Some implementations of the 'wc -l' program in Haskell, with an eye to C-like
performance. This illustrates the balance to be made between performance and
elegance, over several increasingly fast (and more complex) examples.

0.025s, a bit less than 2x slower than C. Not too bad, and probably a
satisfactory place to stop optimising in normal circumstances. Its also nice
that the code is similarly concise.

5 Ptr hacking

ByteStrings give you access to the underlying pointers to bytes in memory,
which can be used to optimise some particular code. So when the ByteString api
doesn't provide what you want, you can step inside the ForeignPtr and go nuts.

This example also makes use of a cpp macro to force strictness on a function,
via a seq guard case.

10 Going via C

We reach a point where I can't think of any more tricks, so we can always code
up a little C and call into that, for this tight loop. Sometimes we just have
to do this, and that's what the ffi is for, after all.