So, recently I had a mess around with alternative alias methods...
Generally, most people just do something like this...
alias :method_name_new :method_namedef method_name method_name_newendWhilst this is fine for most cases, after much underground digging I found that this is not an ideal solution. This is due to creating a 'memory leak' by having multiple 'almost identical' methods defined.
After perusing many websites I came across some helpful information for an alternative way to perform an alias...
method_alias = instance_method🙂method_name)method__proc = Proc.new{ method_alias.bind(self).call }send🙂define_method, :method_name, method__proc)At first, I thought, hey this is pretty cool. I can alias methods without leaving any 'spare methods' laying around. However, after performing some basic benchmarking tests (due to something FenixFyeX mentioned about performance) discovered that using such methods to perform an alias (example 2) is significantly slower.
Here is my test code..
# Alias Speed Test..module TesT class << self def method_a end alias :method_a_alias :method_a def method_a method_a_alias end def method_b end method_b_alias = instance_method🙂method_ method_b_proc = Proc.new{ method_b_alias.bind(self).call } send🙂define_method, :method_b, method_b_proc) endendBenchmark.ips do |x| x.report('one') { TesT.method_a } x.report('two') { TesT.method_b } x.compare!end# Results ::# Calculating -------------------------------------# one 53.845k i/100ms# two 40.283k i/100ms#-------------------------------------------------# one 2.588M (± 2.7%) i/s - 12.977M# two 1.000M (±10.8%) i/s - 4.794M# Comparison:# one: 2588272.8 i/s# two: 1000330.8 i/s - 2.59x slowerAs you can see, using the second alias technique is so much slower when being called, which makes me wonder, which method I should use?
Personally, I strive for efficiency, which leads me to want to use method_a, but the fact that it is leaving the aforementioned 'spare methods' hanging around, is it worth it?
Also, does anyone have an even better (and faster) alternative?
Generally, most people just do something like this...
alias :method_name_new :method_namedef method_name method_name_newendWhilst this is fine for most cases, after much underground digging I found that this is not an ideal solution. This is due to creating a 'memory leak' by having multiple 'almost identical' methods defined.
After perusing many websites I came across some helpful information for an alternative way to perform an alias...
method_alias = instance_method🙂method_name)method__proc = Proc.new{ method_alias.bind(self).call }send🙂define_method, :method_name, method__proc)At first, I thought, hey this is pretty cool. I can alias methods without leaving any 'spare methods' laying around. However, after performing some basic benchmarking tests (due to something FenixFyeX mentioned about performance) discovered that using such methods to perform an alias (example 2) is significantly slower.
Here is my test code..
# Alias Speed Test..module TesT class << self def method_a end alias :method_a_alias :method_a def method_a method_a_alias end def method_b end method_b_alias = instance_method🙂method_ method_b_proc = Proc.new{ method_b_alias.bind(self).call } send🙂define_method, :method_b, method_b_proc) endendBenchmark.ips do |x| x.report('one') { TesT.method_a } x.report('two') { TesT.method_b } x.compare!end# Results ::# Calculating -------------------------------------# one 53.845k i/100ms# two 40.283k i/100ms#-------------------------------------------------# one 2.588M (± 2.7%) i/s - 12.977M# two 1.000M (±10.8%) i/s - 4.794M# Comparison:# one: 2588272.8 i/s# two: 1000330.8 i/s - 2.59x slowerAs you can see, using the second alias technique is so much slower when being called, which makes me wonder, which method I should use?
Personally, I strive for efficiency, which leads me to want to use method_a, but the fact that it is leaving the aforementioned 'spare methods' hanging around, is it worth it?
Also, does anyone have an even better (and faster) alternative?