Skip to content

Instantly share code, notes, and snippets.

@czj
Created June 23, 2020 10:30
Show Gist options
  • Save czj/fe5f069f7598edcb5f4d393c5db29e60 to your computer and use it in GitHub Desktop.
Save czj/fe5f069f7598edcb5f4d393c5db29e60 to your computer and use it in GitHub Desktop.
Comparing Ruby includes?() versus member?() performance
# frozen_string_literal: true
require "benchmark"
array = [4, 5, 6, 11, 12, 20, 40, 64, 65, 66, 81, 83].freeze
set = Set.new(array).freeze
n = 10_000_000
Benchmark.bm(10) do |x|
x.report("INCLUDE array found at position 0") { n.times { array.include?(4) } }
x.report("MEMBER array found at position 0") { n.times { array.member?(4) } }
x.report("INCLUDE array found at position 3") { n.times { array.include?(4) } }
x.report("MEMBER array found at position 3") { n.times { array.member?(4) } }
x.report("INCLUDE array found at position 6") { n.times { array.include?(20) } }
x.report("MEMBER array found at position 6") { n.times { array.member?(20) } }
x.report("INCLUDE array found at position 12") { n.times { array.include?(83) } }
x.report("MEMBER array found at position 12") { n.times { array.member?(83) } }
end
Benchmark.bm(10) do |x|
x.report("INCLUDE set found at position 0") { n.times { set.include?(4) } }
x.report("MEMBER set found at position 0") { n.times { set.member?(4) } }
x.report("INCLUDE set found at position 3") { n.times { set.include?(4) } }
x.report("MEMBER set found at position 3") { n.times { set.member?(4) } }
x.report("INCLUDE set found at position 6") { n.times { set.include?(20) } }
x.report("MEMBER set found at position 6") { n.times { set.member?(20) } }
x.report("INCLUDE set found at position 12") { n.times { set.include?(83) } }
x.report("MEMBER set found at position 12") { n.times { set.member?(83) } }
end
# ARRAY
INCLUDE array found at position 0 0.435590 0.000751 0.436341 ( 0.436900)
MEMBER array found at position 0 1.842729 0.008922 1.851651 ( 1.852418)
INCLUDE array found at position 3 0.428760 0.000787 0.429547 ( 0.429682)
MEMBER array found at position 3 1.812434 0.002646 1.815080 ( 1.815789)
INCLUDE array found at position 6 0.647740 0.000819 0.648559 ( 0.648889)
MEMBER array found at position 6 2.501011 0.003785 2.504796 ( 2.506374)
INCLUDE array found at position 12 0.939216 0.001906 0.941122 ( 0.942874)
MEMBER array found at position 12 3.236601 0.004980 3.241581 ( 3.244097)
# SET
INCLUDE set found at position 0 0.498498 0.000447 0.498945 ( 0.499263)
MEMBER set found at position 0 0.500765 0.001357 0.502122 ( 0.503197)
INCLUDE set found at position 3 0.498391 0.001104 0.499495 ( 0.499727)
MEMBER set found at position 3 0.499100 0.000217 0.499317 ( 0.499470)
INCLUDE set found at position 6 0.498122 0.000302 0.498424 ( 0.498533)
MEMBER set found at position 6 0.500209 0.000485 0.500694 ( 0.501011)
INCLUDE set found at position 12 0.497949 0.000381 0.498330 ( 0.498489)
MEMBER set found at position 12 0.504947 0.000892 0.505839 ( 0.506567)

member? is a method of Enumerable, which is WAY slower for Array but not for Set

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