4/6 C vs. Java vs. Perl comparisons:
------------------
#include <stdio.h>
#include <stdlib.h>
int fib(int num) {
if (num==0 || num==1) {return 1;}
return fib(num-1) + fib(num-2);
}
main(int argc, char *argv[]) {
printf("%d\n", fib(atoi(argv[1])));
exit(0);
}
------------------
import java.lang.*;
class fib {
private static int fib(int num) {
if (num==0 || num==1) {return 1;}
return fib(num-1)+fib(num-2);
}
public static void main(String args[]) {
System.out.println(fib( (new Integer(args[0])).intValue() ));
}
}
------------------
#!/usr/local/bin/perl5
sub fib {
my($num)=@_;
if ($num==0 || $num==1) {return 1;}
return fib($num-1)+fib($num-2);
}
print fib($ARGV[0])."\n";
Conclusion: C is still the fastest. Java is not as slow as people
think it is. Perl is nowhere close to Java performance.
\_ I could an implementation on my TI-85 pseudo-BASIC that will kick
the crap out of any of those. Take an algorithms course. That's
the conclusion. |