Skip to content

Instantly share code, notes, and snippets.

@belfazt
Created May 24, 2018
Embed
What would you like to do?
Benchmarking sets against arrays
require 'benchmark/ips' # gem install benchmark-ips
require 'set'
first_element = 1
last_element = 1000
middle_element = (last_element - first_element) / 2
array = (first_element..last_element).to_a
set = array.to_set
{
first_element: first_element,
middle_element: middle_element,
last_element: last_element,
middle_element_and_last_element: [middle_element, last_element],
half_of_the_elements: (first_element..last_element).step(2).to_a,
element_not_in_the_collection: last_element + 1,
elements_not_in_the_collection: Array.new(200000) { |_| last_element + 1 }
}.each do |k, v|
Benchmark.ips do |x|
x.report("Array#include?(#{k}) -> #{Array(v).size}") { Array(v).each { |element| array.include?(element) } }
x.report("Set#include?(#{k}) -> #{Array(v).size}") { Array(v).each { |element| set.include?(element) } }
x.report("Array#to_set#include?(#{k}) -> #{Array(v).size}") do
set2 = array.to_set
Array(v).each { |element| set2.include?(element) }
end
x.compare!
end
end
Warming up --------------------------------------
Array#include?(first_element) -> 1
240.145k i/100ms
Set#include?(first_element) -> 1
229.303k i/100ms
Array#to_set#include?(first_element) -> 1
1.027k i/100ms
Calculating -------------------------------------
Array#include?(first_element) -> 1
4.009M (± 3.4%) i/s - 20.172M in 5.038192s
Set#include?(first_element) -> 1
3.429M (± 9.0%) i/s - 17.198M in 5.063154s
Array#to_set#include?(first_element) -> 1
10.107k (± 4.2%) i/s - 51.350k in 5.090553s
Comparison:
Array#include?(first_element) -> 1: 4008613.4 i/s
Set#include?(first_element) -> 1: 3429086.3 i/s - 1.17x slower
Array#to_set#include?(first_element) -> 1: 10106.9 i/s - 396.62x slower
Warming up --------------------------------------
Array#include?(middle_element) -> 1
32.874k i/100ms
Set#include?(middle_element) -> 1
221.753k i/100ms
Array#to_set#include?(middle_element) -> 1
930.000 i/100ms
Calculating -------------------------------------
Array#include?(middle_element) -> 1
343.217k (± 5.1%) i/s - 1.742M in 5.092597s
Set#include?(middle_element) -> 1
3.500M (± 5.9%) i/s - 17.518M in 5.024861s
Array#to_set#include?(middle_element) -> 1
9.431k (± 5.5%) i/s - 47.430k in 5.045726s
Comparison:
Set#include?(middle_element) -> 1: 3500104.3 i/s
Array#include?(middle_element) -> 1: 343217.5 i/s - 10.20x slower
Array#to_set#include?(middle_element) -> 1: 9430.7 i/s - 371.14x slower
Warming up --------------------------------------
Array#include?(last_element) -> 1
17.066k i/100ms
Set#include?(last_element) -> 1
221.189k i/100ms
Array#to_set#include?(last_element) -> 1
981.000 i/100ms
Calculating -------------------------------------
Array#include?(last_element) -> 1
177.833k (± 1.7%) i/s - 904.498k in 5.087751s
Set#include?(last_element) -> 1
3.593M (± 3.1%) i/s - 18.137M in 5.052787s
Array#to_set#include?(last_element) -> 1
9.921k (± 2.0%) i/s - 50.031k in 5.044705s
Comparison:
Set#include?(last_element) -> 1: 3593048.0 i/s
Array#include?(last_element) -> 1: 177833.2 i/s - 20.20x slower
Array#to_set#include?(last_element) -> 1: 9921.4 i/s - 362.15x slower
Warming up --------------------------------------
Array#include?(middle_element_and_last_element) -> 2
11.906k i/100ms
Set#include?(middle_element_and_last_element) -> 2
249.859k i/100ms
Array#to_set#include?(middle_element_and_last_element) -> 2
983.000 i/100ms
Calculating -------------------------------------
Array#include?(middle_element_and_last_element) -> 2
121.606k (± 1.6%) i/s - 619.112k in 5.092447s
Set#include?(middle_element_and_last_element) -> 2
4.218M (± 3.0%) i/s - 21.238M in 5.039104s
Array#to_set#include?(middle_element_and_last_element) -> 2
9.911k (± 2.3%) i/s - 50.133k in 5.060975s
Comparison:
Set#include?(middle_element_and_last_element) -> 2: 4218436.1 i/s
Array#include?(middle_element_and_last_element) -> 2: 121606.1 i/s - 34.69x slower
Array#to_set#include?(middle_element_and_last_element) -> 2: 9911.4 i/s - 425.62x slower
Warming up --------------------------------------
Array#include?(half_of_the_elements) -> 500
74.000 i/100ms
Set#include?(half_of_the_elements) -> 500
2.879k i/100ms
Array#to_set#include?(half_of_the_elements) -> 500
746.000 i/100ms
Calculating -------------------------------------
Array#include?(half_of_the_elements) -> 500
750.724 (± 1.9%) i/s - 3.774k in 5.028794s
Set#include?(half_of_the_elements) -> 500
29.529k (± 3.6%) i/s - 149.708k in 5.077767s
Array#to_set#include?(half_of_the_elements) -> 500
7.420k (± 2.5%) i/s - 37.300k in 5.030775s
Comparison:
Set#include?(half_of_the_elements) -> 500: 29528.6 i/s
Array#to_set#include?(half_of_the_elements) -> 500: 7419.5 i/s - 3.98x slower
Array#include?(half_of_the_elements) -> 500: 750.7 i/s - 39.33x slower
Warming up --------------------------------------
Array#include?(element_not_in_the_collection) -> 1
17.084k i/100ms
Set#include?(element_not_in_the_collection) -> 1
224.832k i/100ms
Array#to_set#include?(element_not_in_the_collection) -> 1
982.000 i/100ms
Calculating -------------------------------------
Array#include?(element_not_in_the_collection) -> 1
177.393k (± 1.6%) i/s - 888.368k in 5.009281s
Set#include?(element_not_in_the_collection) -> 1
3.616M (± 2.9%) i/s - 18.211M in 5.040977s
Array#to_set#include?(element_not_in_the_collection) -> 1
9.947k (± 1.9%) i/s - 50.082k in 5.036780s
Comparison:
Set#include?(element_not_in_the_collection) -> 1: 3615766.8 i/s
Array#include?(element_not_in_the_collection) -> 1: 177392.8 i/s - 20.38x slower
Array#to_set#include?(element_not_in_the_collection) -> 1: 9947.1 i/s - 363.50x slower
Warming up --------------------------------------
Array#include?(elements_not_in_the_collection) -> 200000
1.000 i/100ms
Set#include?(elements_not_in_the_collection) -> 200000
7.000 i/100ms
Array#to_set#include?(elements_not_in_the_collection) -> 200000
7.000 i/100ms
Calculating -------------------------------------
Array#include?(elements_not_in_the_collection) -> 200000
0.929 (± 0.0%) i/s - 5.000 in 5.382232s
Set#include?(elements_not_in_the_collection) -> 200000
73.717 (± 2.7%) i/s - 371.000 in 5.036545s
Array#to_set#include?(elements_not_in_the_collection) -> 200000
74.616 (± 2.7%) i/s - 378.000 in 5.068880s
Comparison:
Array#to_set#include?(elements_not_in_the_collection) -> 200000: 74.6 i/s
Set#include?(elements_not_in_the_collection) -> 200000: 73.7 i/s - same-ish: difference falls within error
Array#include?(elements_not_in_the_collection) -> 200000: 0.9 i/s - 80.32x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment