Hmm.... so the new method made by define_method is slower than any regular method if they're called?
depends...
# Not really any noticeable performance lossdefine_method
🙂name) do |*args| args.each(&
😛rint)end# Considerable performance loss due to the use of proc.proc = Proc.new {|*args| args.each(&
😛rint) }send
🙂define_method,:name, proc)Also, imo - if you can justify it, use it.
I mean...
[*1..12].each do |digit| proc = Proc.new { trigger?("VK_F#{digit}".to_sym) } send
🙂define_method, "f#{digit}?", proc) end^ There was no way in hell I was writing that out manually. For this, the amount of time the methods may* be used (*could be never) is very small, so any potential performance lost would be minimal.
Edit:
Just done some simple benchmark comparison and uhh... Seems my previous statement was sightly inaccurate. So I shall change it..
* There is more chance of you incurring unnecessary overhead by using a proc. The performance difference in minimal when used sparingly/effectively. However, compared to a normal method definition, it is undoubtedly slower.
(test results in spoiler)
Spoiler
This is the test results of the example (below result) of using define_method.
Code:
Calculating ------------------------------------- A 46.898k i/100ms B 44.005k i/100ms C 50.202k i/100ms------------------------------------------------- A 1.459M (± 5.6%) i/s - 14.538M B 1.456M (± 6.1%) i/s - 14.522M C 1.776M (± 6.8%) i/s - 17.671MComparison: C: 1776229.6 i/s A: 1458942.9 i/s - 1.22x slower B: 1455862.6 i/s - 1.22x slower
Code:
module TEST class << self define_method(:mthoA) do |*args| args.count end proc = Proc.new { |*args| args.count } send(:define_method,:mthoB, proc) def mthoC(*args) args.count end endend