Skip to content

Instantly share code, notes, and snippets.

@dwbutler
Created March 26, 2014 00:03
Show Gist options
  • Save dwbutler/9774171 to your computer and use it in GitHub Desktop.
Save dwbutler/9774171 to your computer and use it in GitHub Desktop.
Benchmark a constant vs a method
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