Guest User

Untitled

a guest
May 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. require 'benchmark'
  2.  
  3. class Clock
  4. def on!
  5. 1 + 2
  6. end
  7.  
  8. define_method(:off!) do
  9. 1 + 2
  10. end
  11. end
  12.  
  13. class AnotherClock < Clock
  14. def on!
  15. super + 3
  16. end
  17.  
  18. define_method(:off!) do
  19. super() + 3
  20. end
  21. end
  22.  
  23. def func
  24. 1 + 2
  25. end
  26.  
  27. def another_func
  28. func + 3
  29. end
  30.  
  31. N = 1000000
  32. clock = Clock.new
  33. another_clock = AnotherClock.new
  34.  
  35. Benchmark.bm(20) do |x|
  36. x.report("Clock#on!") { N.times { clock.on! } }
  37. x.report("AnotherClock#on!") { N.times { another_clock.on! } }
  38. x.report("Clock#off!") { N.times { clock.off! } }
  39. x.report("AnotherClock#off!") { N.times { another_clock.off! } }
  40. x.report("func") { N.times { func } }
  41. x.report("another_func") { N.times { another_func } }
  42. end
Add Comment
Please, Sign In to add comment