Skip to content

Instantly share code, notes, and snippets.

@bradhe
Last active December 12, 2015 06:58
Show Gist options
  • Select an option

  • Save bradhe/4732860 to your computer and use it in GitHub Desktop.

Select an option

Save bradhe/4732860 to your computer and use it in GitHub Desktop.
Timing results of generating the first 40 fibonacci numbers using Topaz Ruby, MRI 1.9.3-p374, Rubinius 2.x, Go 1.0.2, C w/ clang, and C w/ GCC.
$ cc -v
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.1
Thread model: posix
$ time ./a.out
n=0 1
n=1 1
n=2 1
n=3 2
n=4 3
n=5 5
n=6 8
n=7 13
n=8 21
n=9 34
n=10 55
n=11 89
n=12 144
n=13 233
n=14 377
n=15 610
n=16 987
n=17 1597
n=18 2584
n=19 4181
n=20 6765
n=21 10946
n=22 17711
n=23 28657
n=24 46368
n=25 75025
n=26 121393
n=27 196418
n=28 317811
n=29 514229
n=30 832040
n=31 1346269
n=32 2178309
n=33 3524578
n=34 5702887
n=35 9227465
n=36 14930352
n=37 24157817
n=38 39088169
n=39 63245986
real 0m0.741s
user 0m0.715s
sys 0m0.025s
#include <stdio.h>
int fib(int n) {
return n <= 2 ? 1 : fib(n-1) + fib(n-2);
}
int main(void) {
int i;
for(i = 0; i < 40; i++) {
printf("n=%d\t%d\n", i, fib(i));
}
}
$ cat fib.go
package main
import "fmt"
func fib(n int) int {
if n <= 2 { return 1 }
return fib(n-1) + fib(n-2)
}
func main() {
for i := 0; i < 40; i++ {
fmt.Printf("n=%d\t%d\n", i, fib(i))
}
}
public class Fib {
public static int fib(int n) {
if(n <= 2) {
return 1;
} else {
return fib(n-1) + fib(n-2);
}
}
public static void main(String[] args) {
for(int i = 0; i < 40; i++) {
System.out.println("n="+i+"\t"+fib(i));
}
}
}
def fib(n)
n <= 2 ? 1 : fib(n-1) + fib(n-2)
end
40.times do |i|
puts "n=#{i}\t#{fib(i)}"
end
object Fib {
def fib(n: Int) : Int = {
if(n <= 2) 1 else fib(n-1) + fib(n-2)
}
def main(args: Array[String]) {
for(i <- 1 to 40) {
var n = fib(i)
println("n=" + i + "\t" + n);
}
}
}
$ /usr/bin/gcc-4.2 -v
Using built-in specs.
Target: i686-apple-darwin12
Configured with: /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_lang_apple-gcc42/apple-gcc42/work/objroot/src/configure --disable-checking --prefix=/opt/local --mandir=/opt/local/share/man --enable-languages=c,c++,objc,obj-c++ --libexecdir=/opt/local/libexec/apple-gcc42 --libdir=/opt/local/lib/apple-gcc42 --includedir=/opt/local/include/apple-gcc42 --program-suffix=-apple-4.2 --with-system-zlib --disable-nls --with-slibdir=/usr/lib --build=x86_64-apple-darwin12 --enable-werror-always --program-prefix=i686-apple-darwin12- --host=x86_64-apple-darwin12 --target=i686-apple-darwin12 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3) (MacPorts apple-gcc42 5666.3_9)
$ time ./a.out
n=0 1
n=1 1
n=2 1
n=3 2
n=4 3
n=5 5
n=6 8
n=7 13
n=8 21
n=9 34
n=10 55
n=11 89
n=12 144
n=13 233
n=14 377
n=15 610
n=16 987
n=17 1597
n=18 2584
n=19 4181
n=20 6765
n=21 10946
n=22 17711
n=23 28657
n=24 46368
n=25 75025
n=26 121393
n=27 196418
n=28 317811
n=29 514229
n=30 832040
n=31 1346269
n=32 2178309
n=33 3524578
n=34 5702887
n=35 9227465
n=36 14930352
n=37 24157817
n=38 39088169
n=39 63245986
real 0m0.935s
user 0m0.933s
sys 0m0.002s
$ go version
go version go1.0.2
$ time ./fib
n=0 1
n=1 1
n=2 1
n=3 2
n=4 3
n=5 5
n=6 8
n=7 13
n=8 21
n=9 34
n=10 55
n=11 89
n=12 144
n=13 233
n=14 377
n=15 610
n=16 987
n=17 1597
n=18 2584
n=19 4181
n=20 6765
n=21 10946
n=22 17711
n=23 28657
n=24 46368
n=25 75025
n=26 121393
n=27 196418
n=28 317811
n=29 514229
n=30 832040
n=31 1346269
n=32 2178309
n=33 3524578
n=34 5702887
n=35 9227465
n=36 14930352
n=37 24157817
n=38 39088169
n=39 63245986
real 0m0.816s
user 0m0.813s
sys 0m0.002s
$ java -version
java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01-447-11M4203)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01-447, mixed mode)
$ time java Fib
n=0 1
n=1 1
n=2 1
n=3 2
n=4 3
n=5 5
n=6 8
n=7 13
n=8 21
n=9 34
n=10 55
n=11 89
n=12 144
n=13 233
n=14 377
n=15 610
n=16 987
n=17 1597
n=18 2584
n=19 4181
n=20 6765
n=21 10946
n=22 17711
n=23 28657
n=24 46368
n=25 75025
n=26 121393
n=27 196418
n=28 317811
n=29 514229
n=30 832040
n=31 1346269
n=32 2178309
n=33 3524578
n=34 5702887
n=35 9227465
n=36 14930352
n=37 24157817
n=38 39088169
n=39 63245986
real 0m0.582s
user 0m0.637s
sys 0m0.027s
$ ruby -v
rubinius 2.0.0.rc1 (1.8.7 release yyyy-mm-dd JI) [x86_64-apple-darwin12.2.1]
$ time ruby fib.rb
n=0 1
n=1 1
n=2 1
n=3 2
n=4 3
n=5 5
n=6 8
n=7 13
n=8 21
n=9 34
n=10 55
n=11 89
n=12 144
n=13 233
n=14 377
n=15 610
n=16 987
n=17 1597
n=18 2584
n=19 4181
n=20 6765
n=21 10946
n=22 17711
n=23 28657
n=24 46368
n=25 75025
n=26 121393
n=27 196418
n=28 317811
n=29 514229
n=30 832040
n=31 1346269
n=32 2178309
n=33 3524578
n=34 5702887
n=35 9227465
n=36 14930352
n=37 24157817
n=38 39088169
n=39 63245986
real 0m8.922s
user 0m8.908s
sys 0m0.063s
$ ruby -v
ruby 1.9.3p374 (2013-01-15 revision 38858) [x86_64-darwin12.2.1]
$ time ruby fib.rb
n=0 1
n=1 1
n=2 1
n=3 2
n=4 3
n=5 5
n=6 8
n=7 13
n=8 21
n=9 34
n=10 55
n=11 89
n=12 144
n=13 233
n=14 377
n=15 610
n=16 987
n=17 1597
n=18 2584
n=19 4181
n=20 6765
n=21 10946
n=22 17711
n=23 28657
n=24 46368
n=25 75025
n=26 121393
n=27 196418
n=28 317811
n=29 514229
n=30 832040
n=31 1346269
n=32 2178309
n=33 3524578
n=34 5702887
n=35 9227465
n=36 14930352
n=37 24157817
n=38 39088169
n=39 63245986
real 0m18.893s
user 0m18.842s
sys 0m0.043s
$ scala -version
Scala code runner version 2.10.1 -- Copyright 2002-2013, LAMP/EPFL
$ time scala Fib
n=0 1
n=1 1
n=2 1
n=3 2
n=4 3
n=5 5
n=6 8
n=7 13
n=8 21
n=9 34
n=10 55
n=11 89
n=12 144
n=13 233
n=14 377
n=15 610
n=16 987
n=17 1597
n=18 2584
n=19 4181
n=20 6765
n=21 10946
n=22 17711
n=23 28657
n=24 46368
n=25 75025
n=26 121393
n=27 196418
n=28 317811
n=29 514229
n=30 832040
n=31 1346269
n=32 2178309
n=33 3524578
n=34 5702887
n=35 9227465
n=36 14930352
n=37 24157817
n=38 39088169
n=39 63245986
real 0m0.773s
user 0m0.838s
sys 0m0.048s
$ bin/topaz -v
topaz (ruby-1.9.3p125) [x86_64-darwin]
$ time bin/topaz fib.rb
n=0 1
n=1 1
n=2 1
n=3 2
n=4 3
n=5 5
n=6 8
n=7 13
n=8 21
n=9 34
n=10 55
n=11 89
n=12 144
n=13 233
n=14 377
n=15 610
n=16 987
n=17 1597
n=18 2584
n=19 4181
n=20 6765
n=21 10946
n=22 17711
n=23 28657
n=24 46368
n=25 75025
n=26 121393
n=27 196418
n=28 317811
n=29 514229
n=30 832040
n=31 1346269
n=32 2178309
n=33 3524578
n=34 5702887
n=35 9227465
n=36 14930352
n=37 24157817
n=38 39088169
n=39 63245986
real 0m7.514s
user 0m7.366s
sys 0m0.144s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment