Skip to content

Different results for fib benchmark #1325

@ghost

Description

I'm a new and enthusiastic user of Julia. However, in trying some benchmarks, I seem to get Fibonacci being orders of magnitude faster with C than Julia. If anyone could diagnose why, I'd be interested.

The C code I am using is

double fib(int n){
  if(n<=2)
    return(1.0);
  else
    return(fib(n-2)+fib(n-1));
}

main(int argc, char * argv[]){
  struct timeval t0;
  struct timeval t1;
  int num = atoi(argv[1]);
  gettimeofday(&t0, 0);
  double f = fib(num);
  gettimeofday(&t1, 0);
  printf("time in c (fib): %f \n", t1.tv_sec-t0.tv_sec + (t1.tv_usec-t0.tv_usec)/1000000.0);
  printf("f: %f \n", f);
}

While for Julia I tried 2 different definitions

function fib(n)
     if n <= 2
            1.0
     else
        fib(n-1)+fib(n-2);
     end
end
fib2(n) = n < 2 ? n : fib2(n-1) + fib2(n-2)

function mytest()
         gc()
     tic()
         fib(35)
         time = toc()
     println("time in julia (fib): ",time)

         gc()
     tic()
         fib2(35)
         time = toc()
     println("time in julia (fib2): ",time)
end

Now, I know that Julia is timing the print statement here, but I tried fixing that, and it makes no difference. I also tried adding types to the Julia function, again with no difference.

The results I get show Julia far behind:

time in c (fib): 0.005555 
time in julia (fib): 0.26958513259887695
time in julia (fib2): 0.2444448471069336

What gives? For details, this is on a MacBook Pro, with OS X 10.7.4, I used gcc -O2, and tried Julia with both a precompiled binary, and one I built myself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis change adds or pertains to documentationperformanceMust go faster

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions