
Inspired by a discussion on freenode #haskell, I tried to write the fastest Fibonacci number function possible, i.e. given a natural number input n to compute F_n. mlton seems to enjoy substantially better speed despite equivalent algorithms; it may be enlightening to determine the causes of this, at least for those concerned about performance and the inner workings of the Haskell runtime system(s). In general, I am not usually very concerned about performance, nor am I in this case. But it's something of a mindless microbenchmark or similar. The things I'd normally think of to speed this up would be getting some primitives to find the highest bit of an integer (floor . lg) and to test a given bit of an integer (something vaguely like fromEnum . fromIntegral . (`mod` 2) . (/2^k)), both in some lightweight O(1) with low-hidden-constant manner. This doesn't appear to be a factor in the testing I did, as they're largely 1:1 translations of each other. Still, such things would be useful in various other algorithms, of far greater importance than this one. For the moment, mlton-generated binaries crash computing fib (10^8-1), and there is a 6:1 speed difference for fib (10^7-1) between the two, where mlton-generated binaries take just under 1 minute, and ghc- generated binaries take just under 6 minutes. Anyway, thoughts on how to improve all this from the programmer's point of view, or otherwise explaining what's going on or ameliorating whatever effect is at work here would be appreciated. -- wli