Created
March 26, 2014 00:03
-
-
Save dwbutler/9774171 to your computer and use it in GitHub Desktop.
Benchmark a constant vs a method
This file contains 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
RUBY_VERSION | |
# => "2.1.1" | |
require 'benchmark' | |
DEFAULT_OPTIONS = {validate: true} | |
def default_options | |
{validate: true} | |
end | |
Benchmark.benchmark do |bm| | |
bm.report("constant") { 1_000_000.times { DEFAULT_OPTIONS.merge(validate: false) }} | |
bm.report("method") { 1_000_000.times { default_options.merge(validate: false) } } | |
end | |
# constant 1.930000 0.380000 2.310000 ( 2.321628) | |
# method 2.990000 0.550000 3.540000 ( 3.571988) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment