Created
July 22, 2013 16:42
-
-
Save wangjohn/6055413 to your computer and use it in GitHub Desktop.
Performance Benchmark for Refactoring of class_attribute Method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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