Last active
December 29, 2015 07:39
-
-
Save akshay-vishnoi/7638030 to your computer and use it in GitHub Desktop.
Benchmark #options_for_select from form_options_helper.rb
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
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