Skip to content

Instantly share code, notes, and snippets.

@wangjohn
Created July 22, 2013 16:42
Show Gist options
  • Select an option

  • Save wangjohn/6055413 to your computer and use it in GitHub Desktop.

Select an option

Save wangjohn/6055413 to your computer and use it in GitHub Desktop.
Performance Benchmark for Refactoring of class_attribute Method
require 'active_support/core_ext/class/attribute.rb'
require 'benchmark'
NUM_TRIALS = 1000000
Benchmark.bm(25) do |x|
x.report("Defining attribute") do
NUM_TRIALS.times do
class MyClass
class_attribute :my_attribute
end
end
end
x.report("Accessing attribute") do
NUM_TRIALS.times do
MyClass.my_attribute = 5
MyClass.my_attribute = 10
end
end
class MySubClass < MyClass; end
x.report("Accessing attribute (Subclass)") do
NUM_TRIALS.times do
MySubClass.my_attribute = 10
MySubClass.my_attribute = 15
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment