Skip to content

Instantly share code, notes, and snippets.

@tinogomes
Created January 7, 2011 22:17
Show Gist options
  • Select an option

  • Save tinogomes/770212 to your computer and use it in GitHub Desktop.

Select an option

Save tinogomes/770212 to your computer and use it in GitHub Desktop.
attr_* more fast than defined method
require "benchmark"
CYCLES = 1_000_000
class SomeModel
attr_accessor :attr_names
def method_name
@method_name
end
end
Benchmark.bmbm do |x|
c = SomeModel.new
x.report("attr") { CYCLES.times{ c.attr_names } }
x.report("method") { CYCLES.times{ c.method_name } }
end
# Result
# Rehearsal ------------------------------------------
# attr 0.190000 0.000000 0.190000 ( 0.220931)
# method 0.310000 0.000000 0.310000 ( 0.372111)
# --------------------------------- total: 0.500000sec
#
# user system total real
# attr 0.190000 0.000000 0.190000 ( 0.271583)
# method 0.310000 0.000000 0.310000 ( 0.423103)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment