fibonacci

Benchmark created on


Setup

number = 0;
    
    function rec(n){
      if(n === 0) { return 0; }
      if(n === 1) { return 1; }
      else { return rec(n-2) + rec(n-1); }
    }
    
    function rec_tail(n) {
      function tc_rec(x,y,c){
        if (c === 0) { return x; } 
        else { return tc_rec(y, x+y, c-1); }
      }
      return tc_rec(0,1,n);
    }
    
    function rec_tail_loga(n){
      function tc_rec(a,b,p,q,c){
        if(c === 0){ return b; }
        if(c % 2 === 0){ return tc_rec(a, b, q*q+p*p, q*(q+2*p), c/2); }
        else { return tc_rec(b*q+a*q+a*p, b*p+a*q, p, q, c-1); }
      }
      return tc_rec(1,0,0,1,n);
    }
    
    function iter(n){
      var x=0, y=1, c=0, t;
      while(c !== n){
        t=x; x=y; y+=t; c++;
      }
      return x;
    }

Teardown


    number = 0;
  

Test runner

Ready to run.

Testing in
TestOps/sec
recursive
rec(number++);
if(number > 20) { number = 0; }
ready
tail recursive
rec_tail(number++);
ready
tail recursive (log)
rec_tail_loga(number++);
ready
iterative
iter(number++);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.