mxcl/homebrew — pull request #16334 by inkel (2012-12-01 12:24:56+00:00)
Title: Add CFLAGS='-march=native -O3' when compiling ruby

This flags were selected after reading Andrei Lisnic's post _[Making your ruby fly](http://alisnic.net/blog/making-your-ruby-fly/)_.

Initial benchmarks show some minimal performance improvements when running a simple benchmark that performs:

* recursive calculation of n-th Fibonacci number;
* creation and writing to several temp files.

This is the [benchmark script](https://gist.github.com/4181899#file_thebench.rb):

```ruby
require "benchmark"
require "tempfile"

def fib n
case n
when 0 then 0
when 1 then 1
else
fib(n-1) + fib(n-2)
end
end

n = (ARGV[0] || 10).to_i
t = (ARGV[1] || 1000).to_i

Benchmark.bmbm(20) do |r|
r.report("fib(#{n})") { fib(n) }
r.report("#{t} tempfiles") do
t.times do |i|
tmp = Tempfile.new("bench")
begin
tmp.write i
rescue
ensure
tmp.close
tmp.unlink
end
end
end
end

puts
```

When running the script with the latest ruby from homebrew, I've got the following results:

```
>> /usr/bin/time -l ruby thebench.rb 30 1000
Rehearsal --------------------------------------------------------
fib(30) 0.260000 0.000000 0.260000 ( 0.255475)
1000 tempfiles 0.120000 0.250000 0.370000 ( 0.429621)
----------------------------------------------- total: 0.630000sec

user system total real
fib(30) 0.260000 0.000000 0.260000 ( 0.259108)
1000 tempfiles 0.130000 0.270000 0.400000 ( 0.848950)

1.85 real 0.79 user 0.52 sys
7073792 maximum resident set size
0 average shared memory size
0 average unshared data size
0 average unshared stack size
1743 page reclaims
0 page faults
0 swaps
42 block input operations
85 block output operations
0 messages sent
0 messages received
0 signals received
65 voluntary context switches
1893 involuntary context switches
```

Running the script with a customized version with `CFLAGS="-march=native -O3"` the results are the following:

```
>> /usr/bin/time -l bin/ruby thebench.rb 30 1000
Rehearsal --------------------------------------------------------
fib(30) 0.260000 0.000000 0.260000 ( 0.255356)
1000 tempfiles 0.120000 0.250000 0.370000 ( 0.457608)
----------------------------------------------- total: 0.630000sec

user system total real
fib(30) 0.260000 0.000000 0.260000 ( 0.256357)
1000 tempfiles 0.120000 0.250000 0.370000 ( 0.433369)

1.44 real 0.79 user 0.50 sys
7438336 maximum resident set size
0 average shared memory size
0 average unshared data size
0 average unshared stack size
1832 page reclaims
0 page faults
0 swaps
3 block input operations
71 block output operations
0 messages sent
0 messages received
0 signals received
33 voluntary context switches
1892 involuntary context switches
```

As these benchmarks aren't real use cases I tested against a test suite in a medium sized project, with the following results for default ruby:

```
>> /usr/bin/time -l rake test
280 tests, 994 assertions (280 passed, 0 pending, 0 failed, 0 errored)
Ran in 11.013013 seconds

13.89 real 10.85 user 0.81 sys
51933184 maximum resident set size
0 average shared memory size
0 average unshared data size
0 average unshared stack size
12864 page reclaims
78 page faults
0 swaps
11 block input operations
107 block output operations
39957 messages sent
28111 messages received
0 signals received
1193 voluntary context switches
242 involuntary context switches
```

And optimized ruby:

```
>> /usr/bin/time -l ~/dev/tmp/homebrew/bin/rake test
280 tests, 994 assertions (280 passed, 0 pending, 0 failed, 0 errored)
Ran in 10.628247 seconds
Coverage report generated for Protest to /Users/inkel/dev/citrusbyte/bleuacre/coverage. 1278 / 1686 LOC (75.8%) covered.
11.89 real 10.73 user 0.79 sys
49840128 maximum resident set size
0 average shared memory size
0 average unshared data size
0 average unshared stack size
12267 page reclaims
154 page faults
0 swaps
2 block input operations
31 block output operations
39955 messages sent
28099 messages received
0 signals received
1090 voluntary context switches
203 involuntary context switches
```

Benchmark results are also shared in a [public gist](https://gist.github.com/4181899).

As you can see this results are hardly conclusive of any performance improvements introduced by using those custom CFLAGS, so I'd like to know if somebody else can contribute with benchmarks results, and if there's interest with this pull request I might try and add the falcon patch.

Hope you find this useful.