#!perl -w use strict; use Benchmark; # Fibonnaci Number to calculate. use vars ('$n'); # Make it global, but appease strict. $n = 30; my $iter = 100_000; timethese( $iter, { Binet => '&FibBinet($n)', Golf => '&FibGolf($n)', # Recursive => '&FibRecursive($n)', }); sub FibRecursive { my $index = shift; return 0 if 0 == $index; return 1 if 1 == $index; return FibRecursive( $index - 1 ) + FibRecursive( $index - 2 ); } sub FibGolf { my $index = shift; my $acc = 1; my $tmp; while( $index-- ) { $acc-=$tmp+=$acc*=-1; } return $acc; } sub FibBinet { my $index = shift; my $root5 = sqrt 5; return ( ( ( 1 + $root5 ) / 2 ) ** $index - ( ( 1 - $root5 ) / 2 ) ** $index ) / $root5; }