Last active
December 21, 2015 11:09
-
-
Save benweint/6297355 to your computer and use it in GitHub Desktop.
Stupid Ruby Set benchmark
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'set' | |
require 'benchmark' | |
nerrors = 1 | |
iterations = 1000000 | |
def count_allocations(label, iterations) | |
stat_before = GC.stat | |
yield | |
stat_after = GC.stat | |
delta = stat_after[:total_allocated_object] - stat_before[:total_allocated_object] | |
"#{label} allocated #{delta} objects (#{delta.to_f / iterations} per iteration)" | |
end | |
aux_results = [] | |
Benchmark.bm do |x| | |
x.report("set") do | |
aux_results << count_allocations("set", iterations) do | |
iterations.times do | |
s = Set.new | |
nerrors.times { s << rand } | |
nerrors.times { s.include?(rand) } | |
end | |
end | |
end | |
x.report("array") do | |
aux_results << count_allocations("array", iterations) do | |
iterations.times do | |
a = Array.new | |
nerrors.times { a << rand } | |
nerrors.times { a.include?(rand) } | |
end | |
end | |
end | |
end | |
aux_results.each { |r| puts r } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment