Created
August 29, 2017 17:11
-
-
Save miry/6c244b103ac87b2754f1bc696688fd7a to your computer and use it in GitHub Desktop.
Compare map vs array search for Ruby 2.4.1
This file contains hidden or 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' | |
module FastAttributes | |
TRUE_VALUES_ARRAY = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].freeze | |
FALSE_VALUES_ARRAY = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].freeze | |
TRUE_VALUES = {true => nil, 1 => nil, '1' => nil, 't' => nil, 'T' => nil, 'true' => nil, 'TRUE' => nil, 'on' => nil, 'ON' => nil}.freeze | |
FALSE_VALUES = {false => nil, 0 => nil, '0' => nil, 'f' => nil, 'F' => nil, 'false' => nil, 'FALSE' => nil, 'off' => nil, 'OFF' => nil}.freeze | |
end | |
n = 50000 | |
Benchmark.bm do |x| | |
x.report(:array_true_values) { n.times { FastAttributes::TRUE_VALUES_ARRAY.each {|v| FastAttributes::TRUE_VALUES_ARRAY.include?(v) } } } | |
x.report(:map_true_values) { n.times { FastAttributes::TRUE_VALUES_ARRAY.each {|v| FastAttributes::TRUE_VALUES.key?(v) } } } | |
x.report(:array_wrong_values) { n.times { FastAttributes::FALSE_VALUES_ARRAY.each {|v| FastAttributes::TRUE_VALUES_ARRAY.include?(v) } } } | |
x.report(:map_wrong_values) { n.times { FastAttributes::FALSE_VALUES_ARRAY.each {|v| FastAttributes::TRUE_VALUES.key?(v) } } } | |
x.report(:array_false_values) { n.times { FastAttributes::FALSE_VALUES_ARRAY.each {|v| FastAttributes::FALSE_VALUES_ARRAY.include?(v) } } } | |
x.report(:map_false_values) { n.times { FastAttributes::FALSE_VALUES_ARRAY.each {|v| FastAttributes::FALSE_VALUES.key?(v) } } } | |
end |
This file contains hidden or 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
user system total real | |
array_true_values 0.200000 0.000000 0.200000 ( 0.206493) | |
map_true_values 0.060000 0.010000 0.070000 ( 0.060151) | |
array_wrong_values 0.310000 0.000000 0.310000 ( 0.315665) | |
map_wrong_values 0.050000 0.000000 0.050000 ( 0.053225) | |
array_false_values 0.180000 0.000000 0.180000 ( 0.189455) | |
map_false_values 0.050000 0.000000 0.050000 ( 0.061279) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment