Forums

Some stats for fibonacci using recursion

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Some stats for fibonacci using recursion

Postby todd.a » Fri Jul 10, 2009 4:10 am

On IRC Chuck mentioned http://merbist.com/ and while reading this blog post I came across http://pastie.org/485095. Now we don't write code that utilizes the fibonacci sequence in everyday coding, but the results are indicative of a language's performance, and seems consistent with http://shootout.alioth.debian.org/. Note that the time command returns real, user, and sys. Real is the real elapsed time between invocation and termination, user is the user cpu time, and sys is the system's cpu time.

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
Last edited by todd.a on Thu Apr 19, 2012 9:03 pm, edited 10 times in total.
todd.a
Site Admin
 
Posts: 81
Location: Chicago, IL

Re: Some stats for fibonacci using recursion

Postby Charles » Fri Jul 10, 2009 4:14 am

Of course you can also get Cobra's performance with C#, Java, etc. but the key here is that with Cobra you get the performance and the high-level coding experience.

Thanks for sharing these numbers.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 86 guests

cron