At least in Ruby 1.8.6, anti-aliasing seems to be faster:
#!/usr/local/bin/ruby require 'benchmark' $global_bool = true class Object def first_method? $global_bool end def second_method? first_method? end alias_method :third_method?, :first_method? end Benchmark.bm(7) do |x| x.report("first:") { 1000000.times { first_method? }} x.report("second:") { 1000000.times { second_method? }} x.report("third:") { 1000000.times { third_method? }} end
leads to:
$ ./test.rb user system total real first: 0.281000 0.000000 0.281000 ( 0.282000) second: 0.469000 0.000000 0.469000 ( 0.468000) third: 0.281000 0.000000 0.281000 ( 0.282000)
Obviously, you have one less method (look-up receiver ...). Therefore, for him it seems natural faster.
undur_gongor
source share