Guest User

Untitled

a guest
May 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. require "benchmark"
  2. require "thread"
  3. require "thread/queue"
  4.  
  5. methods = {
  6. push: -> q { N.times { |i| q.push i } },
  7. pop: -> q { N.times { |i| q.pop } },
  8. empty?: -> q {
  9. (N/2).times { |i| q.empty? }
  10. q.push :tmp
  11. (N/2).times { |i| q.empty? }
  12. },
  13. clear: -> q {
  14. (N/2).times { q.clear }
  15. (N/2).times { q << :tmp; q.clear }
  16. },
  17. size: -> q { N.times { q.size } },
  18. num_waiting: -> q { N.times { q.num_waiting } }
  19. }
  20. implementations = [Queue, Thread::Queue]
  21.  
  22. N = 1_000_000
  23. Benchmark.bmbm(14) do |x|
  24. queues = implementations.each_with_object({}) { |klass, h| h[klass] = klass.new }
  25. methods.each_pair { |meth, bench|
  26. implementations.each { |klass|
  27. x.report("#{klass.name[0]}##{meth}") {
  28. bench[queues[klass]]
  29. }
  30. }
  31. }
  32. end
Add Comment
Please, Sign In to add comment