Skip to content

Instantly share code, notes, and snippets.

@eregon
Created July 18, 2010 12:09
Show Gist options
  • Save eregon/480359 to your computer and use it in GitHub Desktop.
Save eregon/480359 to your computer and use it in GitHub Desktop.
require "benchmark"
require "thread"
require "thread/queue"
methods = {
push: -> q { N.times { |i| q.push i } },
pop: -> q { N.times { |i| q.pop } },
empty?: -> q {
(N/2).times { |i| q.empty? }
q.push :tmp
(N/2).times { |i| q.empty? }
},
clear: -> q {
(N/2).times { q.clear }
(N/2).times { q << :tmp; q.clear }
},
size: -> q { N.times { q.size } },
num_waiting: -> q { N.times { q.num_waiting } }
}
implementations = [Queue, Thread::Queue]
N = 1_000_000
Benchmark.bmbm(14) do |x|
queues = implementations.each_with_object({}) { |klass, h| h[klass] = klass.new }
methods.each_pair { |meth, bench|
implementations.each { |klass|
x.report("#{klass.name[0]}##{meth}") {
bench[queues[klass]]
}
}
}
end
Rehearsal -------------------------------------------------
Q#push 0.800000 0.010000 0.810000 ( 0.815865)
T#push 0.350000 0.000000 0.350000 ( 0.359307)
Q#pop 1.730000 0.010000 1.740000 ( 1.777996)
T#pop 0.330000 0.000000 0.330000 ( 0.337168)
Q#empty? 0.200000 0.000000 0.200000 ( 0.203036)
T#empty? 0.320000 0.010000 0.330000 ( 0.318956)
Q#clear 0.590000 0.000000 0.590000 ( 0.586799)
T#clear 0.450000 0.000000 0.450000 ( 0.448253)
Q#size 0.160000 0.000000 0.160000 ( 0.162699)
T#size 0.310000 0.000000 0.310000 ( 0.313020)
Q#num_waiting 0.170000 0.000000 0.170000 ( 0.164615)
T#num_waiting 0.310000 0.000000 0.310000 ( 0.315618)
---------------------------------------- total: 5.750000sec
user system total real
Q#push 0.810000 0.000000 0.810000 ( 0.818967)
T#push 0.350000 0.000000 0.350000 ( 0.369173)
Q#pop 1.710000 0.010000 1.720000 ( 1.719467)
T#pop 0.340000 0.000000 0.340000 ( 0.344720)
Q#empty? 0.200000 0.000000 0.200000 ( 0.202756)
T#empty? 0.310000 0.000000 0.310000 ( 0.317207)
Q#clear 0.590000 0.000000 0.590000 ( 0.586336)
T#clear 0.450000 0.000000 0.450000 ( 0.450514)
Q#size 0.160000 0.000000 0.160000 ( 0.162851)
T#size 0.310000 0.000000 0.310000 ( 0.316331)
Q#num_waiting 0.160000 0.010000 0.170000 ( 0.166712)
T#num_waiting 0.310000 0.000000 0.310000 ( 0.315460)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment