Created
January 7, 2011 22:17
-
-
Save tinogomes/770212 to your computer and use it in GitHub Desktop.
attr_* more fast than defined 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 "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