[SOLVED] What runs faster in Ruby: defining the alias method or using alias_method?

Issue

What is faster on later invocation:

def first_method?() second_method?() end

or

alias_method :first method, :second_method

and if possible why?

(NOTE: I don’t ask what is nicer / better etc. -> only raw speed and why it is faster is interesting here)

Solution

a quick look at the source code, will show you the trick:

http://www.ruby-doc.org/core/classes/Module.src/M000447.html

alias_method is written in C. moreover, defining a method in ruby that calls another method, will result in 2 method lookups and calls.

so, alias_method should be faster.

Answered By – Andrea Pavoni

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *