Created
March 29, 2018 20:12
-
-
Save edbond/c024eef4cd2d276dc8528f6506808aee to your computer and use it in GitHub Desktop.
Array include vs Set include
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
# frozen_string_literal: true | |
require 'set' | |
require 'benchmark/ips' | |
ARRAY = 100_000.times.map(&:itself) | |
SET = Set.new(ARRAY) | |
def array_include? | |
ARRAY.include?(50_000) | |
end | |
def set_include? | |
SET.include?(50_000) | |
end | |
N = 10_000 | |
Benchmark.ips do |x| | |
x.report('array') do | |
N.times { array_include? } | |
end | |
x.report('set') do | |
N.times { set_include? } | |
end | |
x.compare! | |
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
Warming up -------------------------------------- | |
array 1.000 i/100ms | |
set 96.000 i/100ms | |
Calculating ------------------------------------- | |
array 0.340 (± 0.0%) i/s - 2.000 in 5.889143s | |
set 960.512 (± 3.4%) i/s - 4.800k in 5.003452s | |
Comparison: | |
set: 960.5 i/s | |
array: 0.3 i/s - 2828.28x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment