Skip to content

Instantly share code, notes, and snippets.

@Mistobaan
Created Nov 6, 2014
Embed
What would you like to do?
ruby include benchmark string vs symbols
require 'benchmark'
require 'set'
# an attempted to remove the .freeze everywhere
class String
alias :f :freeze
end
n = 20000000
item_available_states = ['sold_out', 'on_hold', 'not_yet_available']
item_available_states_freezed = ['sold_out', 'on_hold', 'not_yet_available'].freeze
item_available_states_sym = [:sold_out, :on_hold, :not_yet_available]
item_available_states_sym_freezed = [:sold_out, :on_hold, :not_yet_available].freeze
item_available_states_sym_set = [:sold_out, :on_hold, :not_yet_available].to_set
item_available_states_sym_set_freezed = [:sold_out, :on_hold, :not_yet_available].to_set.freeze
sold_out = "sold_out".freeze
Benchmark.bmbm do |x|
x.report("Test ruby array.includes?(str)") do
n.times do
item_available_states.include?("sold_out")
end
end
x.report("Test ruby array_str_freezed.includes?(str)") do
n.times do
item_available_states_freezed.include?("sold_out")
end
end
x.report("Test ruby array_str_freezed.include?(str_freezed)") do
n.times do
item_available_states_freezed.include?(sold_out)
end
end
x.report("Test ruby array_sym_set.include?(sym)") do
n.times do
item_available_states_sym_set.include?(:sold_out)
end
end
x.report("Test ruby array_sym_set_freezed.include?(sym)") do
n.times do
item_available_states_sym_set_freezed.include?(:sold_out)
end
end
x.report("Test ruby array_sym.include?(sym)") do
n.times do
item_available_states_sym.include?(:sold_out)
end
end
x.report("Test ruby array_sym_freezed.include?(sym)") do
n.times do
item_available_states_sym_freezed.include?(:sold_out)
end
end
end
@Mistobaan
Copy link
Author

ruby bench.rb

Rehearsal -------------------------------------------------------------------------------------
Test ruby array.includes?(str)                      3.800000   0.010000   3.810000 (  3.802493)
Test ruby array_str_freezed.includes?(str)          3.750000   0.000000   3.750000 (  3.759283)
Test ruby array_str_freezed.include?(str_freezed)   2.650000   0.000000   2.650000 (  2.654981)
Test ruby array_sym_set.include?(sym)               2.260000   0.010000   2.270000 (  2.256578)
Test ruby array_sym_set_freezed.include?(sym)       2.300000   0.000000   2.300000 (  2.313008)
Test ruby array_sym.include?(sym)                   1.470000   0.000000   1.470000 (  1.469787)
Test ruby array_sym_freezed.include?(sym)           1.480000   0.000000   1.480000 (  1.485215)
--------------------------------------------------------------------------- total: 17.730000sec

                                                        user     system      total        real
Test ruby array.includes?(str)                      3.780000   0.010000   3.790000 (  3.781804)
Test ruby array_str_freezed.includes?(str)          3.800000   0.000000   3.800000 (  3.803102)
Test ruby array_str_freezed.include?(str_freezed)   2.640000   0.000000   2.640000 (  2.647741)
Test ruby array_sym_set.include?(sym)               2.310000   0.000000   2.310000 (  2.309461)
Test ruby array_sym_set_freezed.include?(sym)       2.300000   0.000000   2.300000 (  2.304020)
Test ruby array_sym.include?(sym)                   1.540000   0.010000   1.550000 (  1.547142)
Test ruby array_sym_freezed.include?(sym)           1.540000   0.000000   1.540000 (  1.541689)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment