Created
July 14, 2014 16:27
-
-
Save sile/b35f9b2d4c2f0be0e273 to your computer and use it in GitHub Desktop.
Fibonacci Number Benchmarks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4) | |
#include <iostream> | |
#include <cstdlib> | |
unsigned fib(unsigned n) { | |
if (n < 2) { | |
return n; | |
} else { | |
return fib(n - 2) + fib(n - 1); | |
} | |
} | |
int main(int argc, char **argv) { | |
unsigned n = atoi(argv[1]); | |
unsigned ret = fib(n); | |
std::cout << "fib(" << n << ") = " << ret << std::endl; | |
return 0; | |
} | |
// $ g++ -O3 fib.cc -o fib | |
// | |
// $ time ./fib 1 | |
// fib(1) = 1 | |
// | |
// real 0m0.002s | |
// user 0m0.001s | |
// sys 0m0.001s | |
// | |
// $ time ./fib 40 | |
// fib(40) = 102334155 | |
// | |
// real 0m0.458s | |
// user 0m0.456s | |
// sys 0m0.000s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% Erlang/OTP 17 [erts-6.1] | |
-module(fib). | |
-export([fib/1]). | |
-compile([native, {hipe, [o3]}]). | |
-spec fib(non_neg_integer()) -> non_neg_integer(). | |
fib(N) when N < 2 -> N; | |
fib(N) -> fib(N - 2) + fib(N - 1). | |
%% $ erl | |
%% | |
%% > c(fib). | |
%% {ok, fib} | |
%% | |
%% > timer:tc(fun () -> fib:fib(40) end). | |
%% {1175278,102334155} % 1.175 seconds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// go version go1.3 linux/amd64 | |
package main | |
func fib(n uint32) uint32 { | |
if n < 2 { return n } else { return fib(n - 2) + fib(n - 1)} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; SBCL 1.2.1 | |
(declaim (inline fib)) | |
(defun fib (n) | |
(declare (optimize (speed 3) (safety 0)) | |
(fixnum n)) | |
(if (< n 2) | |
n | |
(the fixnum (+ (fib (- n 2)) (fib (- n 1)))))) | |
(time (fib 40)) | |
Evaluation took: | |
0.772 seconds of real time | |
0.772883 seconds of total run time (0.771883 user, 0.001000 system) | |
100.13% CPU | |
2,081,064,538 processor cycles | |
0 bytes consed | |
102334155 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "testing" | |
func BenchmarkFib(b *testing.B) { | |
for i := 0; i < b.N; i += 1 { | |
fib(40) | |
} | |
} | |
// go test -bench . | |
// testing: warning: no tests to run | |
// PASS | |
// BenchmarkFib 2 841112488 ns/op // 0.841 seconds | |
// ok _/home/ohta/dev/bench 2.535s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment