Advertisement
Guest User

Untitled

a guest
May 25th, 2015
248
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/ips'
  2.  
  3. class A
  4. def sum
  5. 1 + 1
  6. end
  7. end
  8.  
  9. class B
  10. def sum
  11. eval("1 + 1")
  12. end
  13. end
  14.  
  15. class C
  16. define_method(:sum) {
  17. 1 + 1
  18. }
  19. end
  20.  
  21. class D
  22. def sum
  23. Proc.new { 1 + 1 }.call
  24. end
  25. end
  26.  
  27. class E
  28. def sum
  29. -> { 1 + 1 }.call
  30. end
  31. end
  32.  
  33. Benchmark.ips do |x|
  34. x.config(:time => 20, :warmup => 2)
  35.  
  36. x.report("Using method") do
  37. A.new.sum
  38. end
  39.  
  40. x.report("Using eval") do
  41. B.new.sum
  42. end
  43.  
  44. x.report("Using define_method") do
  45. C.new.sum
  46. end
  47.  
  48. x.report("Using proc") do
  49. D.new.sum
  50. end
  51.  
  52. x.report("Using lambda") do
  53. E.new.sum
  54. end
  55.  
  56. x.compare!
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement