Skip to content

Instantly share code, notes, and snippets.

@Battleroid
Created June 19, 2019 18:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Battleroid/5f15f7d4071b5cc0a7c4ab87b36f8995 to your computer and use it in GitHub Desktop.
Save Battleroid/5f15f7d4071b5cc0a7c4ab87b36f8995 to your computer and use it in GitHub Desktop.
Using events with 6144 elements
Warming up --------------------------------------
each 1.000 i/100ms
map 1.000 i/100ms
map + uniq 1.000 i/100ms
map + set 1.000 i/100ms
chunked by thread 1.000 i/100ms
chunked by thread 1.000 i/100ms
Calculating -------------------------------------
each 2.569 (± 3.7%) i/s - 26.000 in 10.237953s
map 2.379 (± 3.7%) i/s - 24.000 in 10.203514s
map + uniq 2.536 (± 3.7%) i/s - 26.000 in 10.362150s
map + set 1.391 (± 4.4%) i/s - 14.000 in 10.164477s
chunked by thread 0.022 (± 0.0%) i/s - 1.000 in 45.246803s
chunked by thread 0.021 (± 0.0%) i/s - 1.000 in 48.178517s
with 95.0% confidence
Comparison:
each: 2.6 i/s
map + uniq: 2.5 i/s - same-ish: difference falls within error
map: 2.4 i/s - 1.08x (± 0.06) slower
map + set: 1.4 i/s - 1.85x (± 0.11) slower
chunked by thread: 0.0 i/s - 123.81x (± 4.61) slower
with 95.0% confidence
# needs benchmark-ips kalibera
require 'benchmark'
require 'benchmark/ips'
require 'set'
n = 100
events = [
{"something" => "apple"},
{"something" => "apple"},
{"something" => "kiwi"},
{"something" => "kiwi"},
{"something" => "orange"},
{"something" => "orange"}
]
10.times do |_|
events << events.dup
events.flatten!
end
puts "Using events with #{events.count} elements"
Benchmark.ips(10) do |b|
b.config(:stats => :bootstrap) #, :iterations => 3)
b.report('each') do
seen = {}
n.times do
events.each do |e|
s = sprintf("%s", e["something"])
seen[s] = s
end
end
# puts seen
end
b.report('map') do
seen = {}
n.times do
events.map { |e| sprintf("%s", e["something"]) }.each do |s|
seen[s] = s
end
end
# puts seen
end
b.report('map + uniq') do
seen = {}
n.times do
events.map { |e| sprintf("%s", e["something"]) }.uniq.each do |s|
seen[s] = s
end
end
# puts seen
end
b.report('map + set') do
seen = {}
n.times do
Set[events.map { |e| sprintf("%s", e["something"]) }].each do |s|
seen[s] = s
end
end
# puts seen
end
b.report('chunked by thread') do
seen = {}
n.times do
pool = []
events.each_slice(2) do |chunk|
pool << Thread.new(chunk) { |c|
c.each do |e|
s = sprintf("%s", e["something"])
seen[s] = s
end
}
end
pool.map(&:join)
pool.map(&:kill)
end
end
b.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment