2008

2007

2006

2005

2004

Battling Fibonacci

▁ dec 08 2007

There’s been some buzz lately over at programming.reddit.com where they’re comparing the speed of various languages, in calculating the fibonacci sequence.

So today, Vetle (who also has a birthday today, congrats!), posted an example in Simula.

Lets beat it! In D, that is.

import std.stdio;
void main() {
  for(int i = 0; i <= 36; i++) {
    writefln(fib(i));
  }
}
template fib(T) {
  T fib(T n) {
    if (n ==  0 || n == 1) {
      return n;
    } else {
      return cast(T)( fib(n - 1) + fib(n - 2) );
    }
  }
}

Runtime? A mere 0.863 0.742s ;) (with -O)

I can’t be bothered to post other benchmarks. Suffice to say, it’s fast.

UPDATE: Mads beat us with a C version that ran in 0.362s. He cheated by using -O3.

← Previous: Remote editing on OSX  //  Next: Undo commit in subversion (svn)

comments

powered by