Skip to content

Instantly share code, notes, and snippets.

@dwfait
Created October 29, 2019 22:29
Show Gist options
  • Save dwfait/db7f7e077353b9cce0d70ae1f6bb638b to your computer and use it in GitHub Desktop.
Save dwfait/db7f7e077353b9cce0d70ae1f6bb638b to your computer and use it in GitHub Desktop.
# Benchmark when objects are equivelant:
# frozen_string_literal: true
require 'benchmark/ips'
Benchmark.ips do |x|
a1 = (1..10_000).to_a
a2 = (1..10_000).to_a
x.report('old') do
a1.eql? a2
end
x.report('new') do
a1.new_eql? a2
end
x.compare!
end
# RESULTS OBJECTS THE SAME:
Warming up --------------------------------------
old 1.572k i/100ms
new 12.193k i/100ms
Calculating -------------------------------------
old 15.827k (± 1.5%) i/s - 80.172k in 5.066524s
new 129.381k (± 1.7%) i/s - 658.422k in 5.090635s
Comparison:
new: 129380.8 i/s
old: 15827.3 i/s - 8.17x slower
--------------------------
# BENCHMARK WHEN OBJECTS DIFFER:
# frozen_string_literal: true
require 'benchmark/ips'
Benchmark.ips do |x|
a1 = Array.new(100_000, Object.new)
a2 = Array.new(100_000, Object.new)
x.report('old') do
a1.eql? a2
end
x.report('new') do
a1.new_eql? a2
end
x.compare!
end
# OBJECTS DIFFER RESULTS:
Warming up --------------------------------------
old 219.657k i/100ms
new 216.399k i/100ms
Calculating -------------------------------------
old 4.036M (± 4.7%) i/s - 20.208M in 5.021196s
new 4.290M (± 5.0%) i/s - 21.424M in 5.012614s
Comparison:
new: 4290447.8 i/s
old: 4035564.7 i/s - same-ish: difference falls within error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment