Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. require 'benchmark'
  2.  
  3. def fib(n)
  4. n < 2 ? n : fib(n-1) + fib(n-2)
  5. end
  6.  
  7. def test(thread_qty,fib_qty)
  8. puts "\n#{self.class.name}##{__method__} .. thread_qty: #{thread_qty}, fib_qty: #{fib_qty}"
  9. puts Benchmark.measure{
  10. count = 0
  11. arr = []
  12.  
  13. thread_qty.times do |i|
  14. arr[i] = Thread.new {
  15. # sleep(rand/qty)
  16. fib(fib_qty)
  17. Thread.current["mycount"] = count
  18. count += 1
  19. }
  20. end
  21.  
  22. arr.each {|t| t.join; print t["mycount"], ", " }
  23. puts "count = #{count}"
  24. }
  25. puts "\n"
  26. end
  27.  
  28. test(4,1)
  29. test(4,2)
  30. test(4,4)
  31. test(4,8)
  32. test(4,16)
  33. test(4,32)
  34. test(4,48)
  35. # test(4,64)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement