動的メソッド追加パフォーマンス検証
# coding: utf-8
require 'benchmark'
module IncludeMethod
##
# other instance method include your class
def include_instance_method_org(instance, method_names)
self.class.class_eval do
method_names.each do |method_name|
define_method(method_name.to_sym) do |*args|
instance.send(method_name.to_sym, *args)
end
end
end
end
def include_instance_method(instance, method_names)
@instance = instance
method_names.each do |method_name|
self.class.class_eval <<-EOS, __FILE__, __LINE__ + 1
def #{method_name}(*args)
@instance.send(:#{method_name}, *args)
end
EOS
end
end
end
class Klass1
def initialize
end
def agree
puts 'Hello'
end
def hinazuki
puts '馬鹿なの?'
end
def byebye(word = 'したっけ!')
puts word
end
def kusoga(type = 0, user = 1)
if type == 0 && user == 1
puts 'クソが!'
elsif type == 1 && user == 2
puts '君のような感のいいガキは嫌いだよ'
elsif type == 1
puts '111111111'
else
puts 'にゃーん'
end
end
end
class Klass2
include IncludeMethod
def initialize
@klass1 = Klass1.new
method_names = [:agree, :hinazuki, :byebye, :kusoga]
include_instance_method(@klass1, method_names)
end
end
class Klass3
include IncludeMethod
def initialize
@klass1 = Klass1.new
method_names = [:agree, :hinazuki, :byebye, :kusoga]
include_instance_method_org(@klass1, method_names)
end
end
Benchmark.bm do |b|
b.report 'class_eval string' do
klass2 = Klass2.new
p klass2.methods
klass2.agree
klass2.byebye
klass2.byebye('じゃあの')
klass2.kusoga
klass2.kusoga(1)
klass2.kusoga(1, 2)
end
b.report 'define_method' do
klass2 = Klass3.new
p klass2.methods
klass2.agree
klass2.byebye
klass2.byebye('じゃあの')
klass2.kusoga
klass2.kusoga(1)
klass2.kusoga(1, 2)
end
end
user system total real
class_eval string[:klass, :agree, :hinazuki, :byebye, :kusoga, :include_instance_method_org, :include_instance_method, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
Hello
したっけ!
じゃあの
クソが!
111111111
君のような感のいいガキは嫌いだよ
0.000000 0.000000 0.000000 ( 0.000298)
define_method[:agree, :hinazuki, :byebye, :kusoga, :include_instance_method_org, :include_instance_method, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
Hello
したっけ!
じゃあの
クソが!
111111111
君のような感のいいガキは嫌いだよ
0.000000 0.000000 0.000000 ( 0.001054)