Skip to content

Instantly share code, notes, and snippets.

@havenwood havenwood/benchmark.rb Secret

Last active Sep 27, 2019
Embed
What would you like to do?
RougeRRR
require 'benchmark/ips'
require 'set'
Benchmark.ips do |x|
empty = []
exact = %w[foo]
match = %w[foo bar]
short = %w[bar baz]
exact_set = Set['foo']
x.report('Set empty') { empty.to_set > exact_set }
x.report('Set exact') { exact.to_set > exact_set }
x.report('Set match') { match.to_set > exact_set }
x.report('Set short') { short.to_set > exact_set }
x.report('Reg empty') { empty.size > 1 && empty.include?('foo') }
x.report('Reg exact') { exact.size > 1 && exact.include?('foo') }
x.report('Reg match') { match.size > 1 && match.include?('foo') }
x.report('Reg short') { short.size > 1 && short.include?('foo') }
x.compare!
end
Warming up --------------------------------------
Set empty 60.556k i/100ms
Set exact 49.552k i/100ms
Set match 44.953k i/100ms
Set short 43.193k i/100ms
Reg empty 347.789k i/100ms
Reg exact 342.195k i/100ms
Reg match 259.546k i/100ms
Reg short 250.949k i/100ms
Calculating -------------------------------------
Set empty 775.286k (± 4.9%) i/s - 3.876M in 5.011502s
Set exact 621.709k (± 3.7%) i/s - 3.122M in 5.028296s
Set match 508.784k (± 4.4%) i/s - 2.562M in 5.045856s
Set short 505.296k (± 3.5%) i/s - 2.548M in 5.049707s
Reg empty 15.720M (± 3.1%) i/s - 78.600M in 5.005120s
Reg exact 15.796M (± 4.2%) i/s - 79.047M in 5.013349s
Reg match 7.510M (± 5.0%) i/s - 37.634M in 5.024517s
Reg short 6.638M (± 3.5%) i/s - 33.376M in 5.034252s
Comparison:
Reg exact: 15796054.6 i/s
Reg empty: 15719866.3 i/s - same-ish: difference falls within error
Reg match: 7509807.0 i/s - 2.10x slower
Reg short: 6638315.4 i/s - 2.38x slower
Set empty: 775285.6 i/s - 20.37x slower
Set exact: 621709.0 i/s - 25.41x slower
Set match: 508784.1 i/s - 31.05x slower
Set short: 505296.2 i/s - 31.26x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.