Created
July 27, 2011 13:59
-
-
Save christos/1109416 to your computer and use it in GitHub Desktop.
Profile testing options_for_select
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 'rubygems' | |
require 'thread' | |
require 'rails/all' | |
require 'benchmark' | |
include ActionView::Helpers::FormOptionsHelper | |
puts "Using #{`ruby -v`}" | |
@choices = 700.times.collect { [(0...50).map{ ('a'..'z').to_a[rand(26)] }.join, rand(10000)] } | |
module ActionView::Helpers::FormOptionsHelper | |
def faster_options_for_select(container, selected = nil) | |
return container if String === container | |
selected, disabled = extract_selected_and_disabled(selected).map do | r | | |
Array.wrap(r).map {|e| e.to_s} | |
end | |
container.map do |element| | |
html_attributes = option_html_attributes(element) | |
text, value = option_text_and_value(element).map {|e| e.to_s} | |
selected_attribute = ' selected="selected"' if option_value_selected?(value, selected) | |
disabled_attribute = ' disabled="disabled"' if disabled && option_value_selected?(value, disabled) | |
%(<option value="#{ERB::Util.html_escape(value)}"#{selected_attribute}#{disabled_attribute}#{html_attributes}>#{ERB::Util.html_escape(text)}</option>) | |
end.join("\n").html_safe | |
end | |
end | |
COUNT = 100 | |
Benchmark.bmbm do |test| | |
test.report('Using Symbol#to_proc') do | |
COUNT.times do | |
options_for_select(@choices) | |
end | |
end | |
test.report('Standard call') do | |
COUNT.times do | |
faster_options_for_select(@choices) | |
end | |
end | |
end |
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
Using ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin10.7.0], MBARI 0x6770, | |
Ruby Enterprise Edition 2011.03 | |
Rehearsal -------------------------------------------------------- | |
Using Symbol#to_proc 3.520000 0.060000 3.580000 ( 3.594295) | |
Standard call 2.890000 0.020000 2.910000 ( 3.038064) | |
----------------------------------------------- total: 6.490000sec | |
user system total real | |
Using Symbol#to_proc 3.750000 0.060000 3.810000 ( 3.856077) | |
Standard call 2.690000 0.010000 2.700000 ( 2.713586) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment