Skip to content

Instantly share code, notes, and snippets.

@gom
Created July 25, 2018 14:19
Show Gist options
  • Save gom/f9011c10fc68c80d1cc64f546b2db1e5 to your computer and use it in GitHub Desktop.
Save gom/f9011c10fc68c80d1cc64f546b2db1e5 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'set'
# Create random topics
cl = [('a'..'z'), ('A'..'Z'), ('0'..'9')].map { |i| i.to_a }.flatten
topics = ->(m) { (0..rand(m)).map{ (0..1).map { cl[rand(cl.length)] }.join }}
MAX_TOPICS = [5, 10, 20, 50, 100]
TRIALS = 1000000
Benchmark.bmbm do |bm|
MAX_TOPICS.each do |m|
bm.report("Set#delete_if: %s" % m) {
target = Set.new
TRIALS.times {
new_target = Set.new(topics.call(m)).delete_if {|o| target.include? o }
unless new_target.empty?
target.merge(new_target)
end
}
}
bm.report("Unless Set#subset: %s" % m) {
target = Set.new
TRIALS.times {
new_topics = Set.new(topics.call(m))
unless new_topics.subset? target
new_target = Set.new(new_topics) - target
unless new_target.empty?
target.merge(new_target)
end
end
}
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment