Skip to content

Instantly share code, notes, and snippets.

@paul
Created June 28, 2011 20:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save paul/1052128 to your computer and use it in GitHub Desktop.
require 'benchmark'
class Dummy
attr_writer :setter
def orig(att, value)
send("#{att}=", value) if respond_to?("#{att}=")
end
def cache_name(att, value)
method_name = "#{att}="
send(method_name, value) if respond_to?(method_name)
end
def symbolize_method_name(att, value)
method_name = :"#{att}="
send(method_name, value) if respond_to?(method_name)
end
def no_iterp(att, value)
method_name = (att.to_s + '=').to_sym
send(method_name, value) if respond_to?(method_name)
end
end
N = 1_000_000
[:orig, :cache_name, :symbolize_method_name, :no_iterp].each do |method|
@dummy = Dummy.new
Benchmark.bm do |x|
x.report(method.to_s) do
N.times { @dummy.send(method, :setter, 1) }
end
end
end
__END__
user system total real
orig 1.470000 0.000000 1.470000 ( 1.470033)
cache_name 1.040000 0.000000 1.040000 ( 1.035643)
symbolize_method_name 1.030000 0.010000 1.040000 ( 1.033245)
no_iterp 0.910000 0.000000 0.910000 ( 0.913180)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment