Cobra running on mono
- Code: Select all
class Test
def main
.fib(40)
def fib (n as int) as int
if n == 0 or n == 1
return n
else
return .fib(n-1) + .fib(n-2)
real 0m1.204s
user 0m1.180s
sys 0m0.020s
# with -turbo
real 0m1.188s
user 0m1.172s
sys 0m0.008s
C compiled with GCC
- Code: Select all
#include<stdio.h>
static int fib(int n) {
if (n == 0 || n == 1)
return n;
else
return fib(n-1) + fib(n-2);
}
int main() {
printf("%d", fib(40));
return 0;
}
real 0m1.372s
user 0m1.368s
sys 0m0.004s
// with gcc -O3
real 0m0.604s
user 0m0.600s
sys 0m0.000s
Pascal compiled with FPC
- Code: Select all
function fib(n: integer): longint;
begin
if (n = 0) or (n = 1) then
fib := 1
else
fib := fib(n-1) + fib(n-2)
end;
begin
writeln(fib(40))
end.
real 0m1.420s
user 0m1.416s
sys 0m0.000s
with -O3
real 0m1.292s
user 0m1.288s
sys 0m0.000s
v1.8.7 on MRI
- Code: Select all
def fib n
if n == 0 or n == 1
n
else
fib(n-1) + fib(n-2)
end
end
fib(40)
real 10m28.804s
user 5m16.088s
sys 4m48.866s
# v1.9 on YARV/KRI
real 0m34.662s
user 0m33.262s
sys 0m0.004s
PHP 5.2.9
- Code: Select all
function fib($n) {
if ($n == 0 || $n == 1)
return 1;
else
return fib($n-1) + fib($n-2);
}
print fib(40);
real 2m24.264s
user 2m19.265s
sys 0m0.000s
compiled with roadsend 2.9.8
real 1m24.545s
user 1m18.741s
sys 0m0.020s
Python 2.6
- Code: Select all
def fib(n):
if n == 0 || n == 1:
return n
return fib(n-1) + fib(n-2)
print fib(40)
real 1m27.713s
user 1m23.709s
sys 0m0.020s
Scala 2.7.5
- Code: Select all
object Test {
def main(args: Array[String]) {
println(fib(40))
}
def fib(n: Int): Int = {
if (n == 0 || n == 1)
n
else
fib(n-1) + fib(n-2)
}
}
real 0m1.332s
user 0m1.264s
sys 0m0.032s
Go compiled with 6g
- Code: Select all
package main
import fmt "fmt"
func fib(n int) int {
if n == 0 || n == 1 {
return n
}
return fib(n-1) + fib(n-2)
}
func main() {
fmt.Printf("%d", fib(40))
}
real 0m1.344s
user 0m1.256s
sys 0m0.004s