Guest User

Untitled

a guest
Feb 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. require 'benchmark'
  2. include Benchmark
  3.  
  4. class A
  5. def foo(a, b, c)
  6. 1
  7. end
  8. end
  9.  
  10.  
  11. class M1
  12. def method_missing(key, *args, &block)
  13. 1
  14. end
  15. end
  16.  
  17. class M2
  18. def method_missing(key, *args, &block)
  19. if(key == :foo)
  20. send :bar, args[0], args[1], args[2]
  21. end
  22. end
  23.  
  24. def bar(a,b,c)
  25. 1
  26. end
  27.  
  28. end
  29.  
  30. class M3
  31. def method_missing(key, *args, &block)
  32. if(key.to_s.index("foo"))
  33. send :bar, args[0], args[1], args[2]
  34. end
  35. end
  36.  
  37. def bar(a,b,c)
  38. 1
  39. end
  40.  
  41. end
  42.  
  43. class M4
  44. def method_missing(key, *args, &block)
  45. if(key.to_s =~ /foo/)
  46. send :bar, args[0], args[1], args[2]
  47. end
  48. end
  49.  
  50. def bar(a,b,c)
  51. 1
  52. end
  53. end
  54.  
  55. class M5
  56. def method_missing(key, *args, &block)
  57. if(key.to_s =~ /foo/)
  58. self.class.class_eval <<-EOF, __FILE__, __LINE__
  59. def #{key}(a, b, c)
  60. bar a, b, c
  61. end
  62. EOF
  63. send key, args[0], args[1], args[2]
  64. end
  65. end
  66.  
  67. def bar(a,b,c)
  68. 1
  69. end
  70. end
  71.  
  72. class M6
  73. def method_missing(key, *args, &block)
  74. if(key.to_s =~ /foo/)
  75. self.class.class_eval <<-EOF, __FILE__, __LINE__
  76. def #{key}(a, b, c)
  77. 1 # inlining the call
  78. end
  79. EOF
  80. send key, args[0], args[1], args[2]
  81. end
  82. end
  83.  
  84. end
  85.  
  86.  
  87. T = 1_000_000
  88. a = A.new
  89. m1 = M1.new
  90. m2 = M2.new
  91. m3 = M3.new
  92. m4 = M4.new
  93. m5 = M5.new
  94. m6 = M6.new
  95.  
  96. Benchmark.bm(25) do |bm|
  97. bm.report("static method"){T.times{a.foo(1,2,3)}}
  98. bm.report("method missing baseline"){T.times{m1.foo(1,2,3)}}
  99. bm.report("symbol equality"){T.times{m2.foo(1,2,3)}}
  100. bm.report("index check"){T.times{m3.foo(1,2,3)}}
  101. bm.report("regular expression"){T.times{m4.foo(1,2,3)}}
  102. bm.report("caching of method missing"){T.times{m5.foo(1,2,3)}}
  103. bm.report("caching + inlining"){T.times{m6.foo(1,2,3)}}
  104. end
  105.  
  106. # VERSION = 1.8.7-p72
  107. # user system total real
  108. # static method 0.295612 0.001624 0.297236 ( 0.321701)
  109. # method missing baseline 0.567643 0.003783 0.571426 ( 0.628578)
  110. # symbol equality 1.139567 0.006818 1.146385 ( 1.224589)
  111. # index check 1.377338 0.008638 1.385976 ( 1.498267)
  112. # regular expression 1.837666 0.012901 1.850567 ( 2.047866)
  113. # caching of method missing 0.486595 0.002270 0.488865 ( 0.511950)
  114. # caching + inlining 0.365636 0.001684 0.367320 ( 0.383320)
  115. #
  116. # VERSION = 1.9.0-5
  117. # user system total real
  118. # static method 0.287593 0.001519 0.289112 ( 0.308552)
  119. # method missing baseline 0.552172 0.003834 0.556006 ( 0.604434)
  120. # symbol equality 1.180124 0.005763 1.185887 ( 1.260123)
  121. # index check 1.297773 0.007940 1.305713 ( 1.415773)
  122. # regular expression 1.858729 0.012671 1.871400 ( 2.063151)
  123. # caching of method missing 0.457304 0.002010 0.459314 ( 0.478891)
  124. # caching + inlining 0.342845 0.002021 0.344866 ( 0.361926)
  125. #
  126. # VERSION = JRUBY
  127. # user system total real
  128. # static method 0.313397 0.002809 0.316206 ( 0.354470)
  129. # method missing baseline 0.570404 0.004562 0.574966 ( 0.637837)
  130. # symbol equality 1.082941 0.006078 1.089019 ( 1.173902)
  131. # index check 1.256466 0.008117 1.264583 ( 1.395792)
  132. # regular expression 1.627718 0.012748 1.640466 ( 1.872134)
  133. # caching of method missing 0.471655 0.002542 0.474197 ( 0.501606)
  134. # caching + inlining 0.362206 0.001912 0.364118 ( 0.383157)
  135. #
  136. # VERSION = rubinius 0.10.0 (362a70237)
  137. # user system total real
  138. # static method 0.479927 0.000000 0.479927 ( 0.479883)
  139. # method missing baseline 1.024028 0.000000 1.024028 ( 1.024081)
  140. # symbol equality 1.485076 0.000000 1.485076 ( 1.485110)
  141. # index check 10.996608 0.000000 10.996608 ( 10.996657)
  142. # regular expression 6.458455 0.000000 6.458455 ( 6.458505)
  143. # caching of method missing 0.636479 0.000000 0.636479 ( 0.636501)
  144. # caching + inlining Segmentation fault
  145. #
  146. # It's interesting for me looking at different implementations and see how things
  147. # differ from one another. It really shows places where they need improvements.
Add Comment
Please, Sign In to add comment