Skip to content

Instantly share code, notes, and snippets.

@ismaelga
Last active August 29, 2015 14:13
Show Gist options
  • Save ismaelga/68bb3ea51b4742f65699 to your computer and use it in GitHub Desktop.
Save ismaelga/68bb3ea51b4742f65699 to your computer and use it in GitHub Desktop.
Benchmark of Set#include? using Hash#[]
require 'benchmark/ips'
require 'set'
sizes = [10, 100, 1_000, 10_000]
accesses = %i(first last hits misses)
class NewSet < Set
def initialize(enum = nil, &block)
@hash = Hash.new(false)
super
end
def include?(o)
@hash[o]
end
end
accesses.each do |access|
puts "\n\n\n\n"
sizes.each do |size|
puts "#{access} SIZE: #{size}"
vals = (1..size).to_a
set = Set.new(vals)
new_set = NewSet.new(vals)
#GC.start
Benchmark.ips do |x|
case access
when :first then x.report("NewSet#include?") { new_set.include? 1 }
when :last then x.report("NewSet#include?") { new_set.include? size }
when :hits then x.report("NewSet#include?") { size.times {|i| new_set.include? i } }
when :misses then x.report("NewSet#include?") { size.times {|i| new_set.include?(-i) } }
end
case access
when :first then x.report("Set#include?") { set.include? 1 }
when :last then x.report("Set#include?") { set.include? size }
when :hits then x.report("Set#include?") { size.times {|i| set.include? i } }
when :misses then x.report("Set#include?") { size.times {|i| set.include?(-i) } }
end
x.compare!
end
puts "================================================="
end
end
first SIZE: 10
Calculating -------------------------------------
NewSet#include? 132.763k i/100ms
Set#include? 128.940k i/100ms
-------------------------------------------------
NewSet#include? 6.483M (± 6.0%) i/s - 32.394M
Set#include? 5.969M (± 5.9%) i/s - 29.785M
Comparison:
NewSet#include?: 6483127.6 i/s
Set#include?: 5969013.1 i/s - 1.09x slower
=================================================
first SIZE: 100
Calculating -------------------------------------
NewSet#include? 131.179k i/100ms
Set#include? 127.306k i/100ms
-------------------------------------------------
NewSet#include? 5.917M (± 7.0%) i/s - 29.515M
Set#include? 5.397M (± 8.0%) i/s - 26.862M
Comparison:
NewSet#include?: 5917141.6 i/s
Set#include?: 5396610.3 i/s - 1.10x slower
=================================================
first SIZE: 1000
Calculating -------------------------------------
NewSet#include? 129.624k i/100ms
Set#include? 127.803k i/100ms
-------------------------------------------------
NewSet#include? 5.999M (± 5.6%) i/s - 29.943M
Set#include? 5.279M (± 6.6%) i/s - 26.327M
Comparison:
NewSet#include?: 5998870.6 i/s
Set#include?: 5279042.2 i/s - 1.14x slower
=================================================
first SIZE: 10000
Calculating -------------------------------------
NewSet#include? 120.823k i/100ms
Set#include? 118.621k i/100ms
-------------------------------------------------
NewSet#include? 5.157M (± 5.5%) i/s - 25.735M
Set#include? 4.172M (± 6.8%) i/s - 20.759M
Comparison:
NewSet#include?: 5156717.3 i/s
Set#include?: 4171836.4 i/s - 1.24x slower
=================================================
last SIZE: 10
Calculating -------------------------------------
NewSet#include? 128.555k i/100ms
Set#include? 128.087k i/100ms
-------------------------------------------------
NewSet#include? 6.442M (± 6.6%) i/s - 32.139M
Set#include? 5.912M (± 6.0%) i/s - 29.460M
Comparison:
NewSet#include?: 6442200.9 i/s
Set#include?: 5911684.6 i/s - 1.09x slower
=================================================
last SIZE: 100
Calculating -------------------------------------
NewSet#include? 130.143k i/100ms
Set#include? 131.041k i/100ms
-------------------------------------------------
NewSet#include? 6.232M (±10.2%) i/s - 30.844M
Set#include? 5.678M (± 8.3%) i/s - 28.174M
Comparison:
NewSet#include?: 6231554.6 i/s
Set#include?: 5677968.8 i/s - 1.10x slower
=================================================
last SIZE: 1000
Calculating -------------------------------------
NewSet#include? 128.153k i/100ms
Set#include? 129.385k i/100ms
-------------------------------------------------
NewSet#include? 6.365M (± 8.5%) i/s - 31.654M
Set#include? 5.609M (±11.0%) i/s - 27.688M
Comparison:
NewSet#include?: 6364685.4 i/s
Set#include?: 5608880.4 i/s - 1.13x slower
=================================================
last SIZE: 10000
Calculating -------------------------------------
NewSet#include? 132.026k i/100ms
Set#include? 125.038k i/100ms
-------------------------------------------------
NewSet#include? 6.260M (± 8.6%) i/s - 31.158M
Set#include? 5.766M (± 7.9%) i/s - 28.634M
Comparison:
NewSet#include?: 6259955.6 i/s
Set#include?: 5766189.3 i/s - 1.09x slower
=================================================
hits SIZE: 10
Calculating -------------------------------------
NewSet#include? 55.111k i/100ms
Set#include? 50.006k i/100ms
-------------------------------------------------
NewSet#include? 826.759k (± 4.6%) i/s - 4.133M
Set#include? 738.046k (± 4.1%) i/s - 3.700M
Comparison:
NewSet#include?: 826759.4 i/s
Set#include?: 738046.5 i/s - 1.12x slower
=================================================
hits SIZE: 100
Calculating -------------------------------------
NewSet#include? 8.233k i/100ms
Set#include? 7.626k i/100ms
-------------------------------------------------
NewSet#include? 90.177k (± 3.5%) i/s - 452.815k
Set#include? 79.320k (± 3.6%) i/s - 396.552k
Comparison:
NewSet#include?: 90176.8 i/s
Set#include?: 79319.6 i/s - 1.14x slower
=================================================
hits SIZE: 1000
Calculating -------------------------------------
NewSet#include? 902.000 i/100ms
Set#include? 774.000 i/100ms
-------------------------------------------------
NewSet#include? 9.010k (± 5.1%) i/s - 45.100k
Set#include? 7.803k (± 3.1%) i/s - 39.474k
Comparison:
NewSet#include?: 9009.7 i/s
Set#include?: 7803.0 i/s - 1.15x slower
=================================================
hits SIZE: 10000
Calculating -------------------------------------
NewSet#include? 83.000 i/100ms
Set#include? 66.000 i/100ms
-------------------------------------------------
NewSet#include? 841.185 (± 4.2%) i/s - 4.233k
Set#include? 664.548 (± 5.7%) i/s - 3.366k
Comparison:
NewSet#include?: 841.2 i/s
Set#include?: 664.5 i/s - 1.27x slower
=================================================
misses SIZE: 10
Calculating -------------------------------------
NewSet#include? 47.360k i/100ms
Set#include? 47.151k i/100ms
-------------------------------------------------
NewSet#include? 649.390k (±10.7%) i/s - 3.220M
Set#include? 579.233k (±14.5%) i/s - 2.829M
Comparison:
NewSet#include?: 649389.8 i/s
Set#include?: 579232.8 i/s - 1.12x slower
=================================================
misses SIZE: 100
Calculating -------------------------------------
NewSet#include? 5.834k i/100ms
Set#include? 5.778k i/100ms
-------------------------------------------------
NewSet#include? 59.795k (±11.1%) i/s - 297.534k
Set#include? 60.999k (± 6.0%) i/s - 306.234k
Comparison:
Set#include?: 60999.1 i/s
NewSet#include?: 59794.8 i/s - 1.02x slower
=================================================
misses SIZE: 1000
Calculating -------------------------------------
NewSet#include? 652.000 i/100ms
Set#include? 600.000 i/100ms
-------------------------------------------------
NewSet#include? 6.062k (± 8.9%) i/s - 29.992k
Set#include? 6.003k (± 7.4%) i/s - 30.000k
Comparison:
NewSet#include?: 6062.1 i/s
Set#include?: 6002.8 i/s - 1.01x slower
=================================================
misses SIZE: 10000
Calculating -------------------------------------
NewSet#include? 52.000 i/100ms
Set#include? 42.000 i/100ms
-------------------------------------------------
NewSet#include? 513.097 (±12.7%) i/s - 2.548k
Set#include? 472.890 (±15.0%) i/s - 2.352k
Comparison:
NewSet#include?: 513.1 i/s
Set#include?: 472.9 i/s - 1.09x slower
=================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment