Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
$ ./miniruby -v
MacRuby version 0.5 (ruby 1.9.0) [universal-darwin9.0, x86_64]
$ ./miniruby --compile -e "p 42"
$ ./a.out 
42
$ file ./a.out 
./a.out: Mach-O 64-bit executable x86_64
$ otool -L ./a.out 
./a.out:
	/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 677.23.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 227.0.0)
	/usr/lib/libauto.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/lib/libxml2.2.dylib (compatibility version 9.0.0, current version 9.16.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.4)
	/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.4.0)
	/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 476.18.0)
$ du -h ./a.out # omgwtf
 13M	./a.out
$ cat fib.rb 
def fib n
  if n < 3
    1
  else
    fib(n-1) + fib(n-2)
  end
end

n = ARGV.size == 1 ? ARGV.first.to_i : 35
p fib(n)
$ time ./miniruby fib.rb 40
102334155

real	0m3.911s
user	0m3.834s
sys	0m0.050s
$ ./miniruby --compile fib.rb 
$ time ./a.out 40
102334155

real	0m2.959s
user	0m2.900s
sys	0m0.038s
$ cat fib.c
#include <stdio.h>

static int fib(int n)
{
    if (n < 3) {
	return 1;
    }
    else {
	return fib(n - 1) + fib(n - 2);
    }
}

int main(int argc, char **argv)
{
    const int n = argc > 1 ? atoi(argv[1]) : 37;
    printf("%d\n", fib(n));
    return 0;
}
$ gcc fib.c -o fib -arch x86_64
$ time ./fib 40
102334155

real	0m1.755s
user	0m1.732s
sys	0m0.011s
$ gcc fib.c -o fib -arch x86_64 -O3
$ time ./fib 40
102334155

real	0m0.491s
user	0m0.482s
sys	0m0.004s
$ cat ocfib.m
#import <Foundation/Foundation.h>

@interface Fib : NSObject
@end

@implementation Fib

- (int)fib:(int)n
{
    if (n < 3) {
	return 1;
    }
    else {
	return [self fib:n - 1] + [self fib:n - 2];
    }
}

@end

int main(int argc, char **argv)
{
    const int n = argc > 1 ? atoi(argv[1]) : 37;
    Fib *o = [Fib new];
    printf("%d\n", [o fib:n]);
    return 0;
}
$ gcc ocfib.m -o ocfib -framework Foundation -fobjc-gc -arch x86_64
$ time ./ocfib 40
102334155

real	0m3.186s
user	0m3.147s
sys	0m0.017s