Skip to content

Instantly share code, notes, and snippets.

@akshay-vishnoi
Last active December 29, 2015 07:39
Show Gist options
  • Save akshay-vishnoi/7638030 to your computer and use it in GitHub Desktop.
Save akshay-vishnoi/7638030 to your computer and use it in GitHub Desktop.
Benchmark #options_for_select from form_options_helper.rb
require 'benchmark'
def check?(a, value)
Array(a).include? value
end
a = (1..100).to_a
value = 500
disabled = true
html_attributes = {}
TIMES = 500_000
Benchmark.bmbm do |b|
b.report('old') do
TIMES.times do
html_attributes[:selected] = check?(a, value)
html_attributes[:disabled] = disabled && check?(a, value)
end
end
b.report('new') do
TIMES.times do
html_attributes[:selected] = check?(a, value)
html_attributes[:disabled] = disabled && html_attributes[:selected]
end
end
end
Output:
Rehearsal ---------------------------------------
old 4.050000 0.000000 4.050000 ( 4.045806)
new 2.090000 0.000000 2.090000 ( 2.093466)
------------------------------ total: 6.140000sec
user system total real
old 4.070000 0.000000 4.070000 ( 4.064235)
new 2.090000 0.000000 2.090000 ( 2.087229)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment