Skip to content

Instantly share code, notes, and snippets.

@catwell
Last active December 22, 2015 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catwell/6389992 to your computer and use it in GitHub Desktop.
Save catwell/6389992 to your computer and use it in GitHub Desktop.
Definitely *not* a good benchmark. But less ridiculous than http://jabsoft.io/2013/08/29/why-c-still-matters-in-2013-a-simple-example/
#include <stdio.h>
#include <stdlib.h>
typedef unsigned long ulong;
int main(int argc, char *argv[])
{
ulong n = atoi(argv[1]);
ulong a = 1;
ulong b = 0;
ulong t;
ulong i;
ulong m = 1UL << 31;
for(i = 0; i < n; i++) {
t = b;
b = (a+b) % m;
a = t;
}
printf("%lu\n", b);
return 0;
}
local fib = function(n)
local SZ = 2^32
local a, b = 1, 0
for i=1,n do
b, a = (b+a) % SZ, b
end
return b
end
print(fib(tonumber(arg[1])))
def fib(n):
SZ = 2**32
a, b = 1, 0
for i in xrange(n):
b, a = (b+a) % SZ, b
return b
import sys
print fib(int(sys.argv[1]))
[fibo 15:45] n=100000000
[fibo 15:45] time ./fibo $n
1819143227
real 0m1.251s
user 0m1.244s
sys 0m0.003s
[fibo 15:46] time python2 fibo.py $n
1819143227
real 0m13.566s
user 0m13.432s
sys 0m0.020s
[fibo 15:46] time luajit fibo.lua $n
1819143227
real 0m1.028s
user 0m1.019s
sys 0m0.002s
@mallochine
Copy link

but compiling fibo.c with -O3 makes it crazy fast:

$ gcc fibo.c -O3 -o fibo

$ time ./fibo $n
1819143227

real 0m0.077s
user 0m0.076s
sys 0m0.001s

@mallochine
Copy link

While compiling with plan GCC gives 1.4 seconds, compared to luajit's 1.132

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment