Guest User

Untitled

a guest
Apr 25th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. require 'benchmark'
  2.  
  3. #
  4. # Benchmarks for various miscellaneous operations.
  5. #
  6.  
  7. # This set of benchmarks looks at two ways of constructing a logger:
  8. # with an interpolated string that gets operated on given a flag,
  9. # or a block that yields the interpolated string, given a flag.
  10. #
  11. # user system total real
  12. # ** short strings **
  13. # m("string", true) 0.100000 0.000000 0.100000 ( 0.104513)
  14. # m("string", false) 0.100000 0.000000 0.100000 ( 0.102459)
  15. # m(true) # :yield: 0.170000 0.000000 0.170000 ( 0.161769)
  16. # m(false) 0.040000 0.000000 0.040000 ( 0.042795)
  17. #
  18. # ** long strings **
  19. # m("string", true) 0.270000 0.000000 0.270000 ( 0.273281)
  20. # m("string", false) 0.270000 0.000000 0.270000 ( 0.276199)
  21. # m(true) # :yield: 0.320000 0.000000 0.320000 ( 0.316087)
  22. # m(false) 0.040000 0.000000 0.040000 ( 0.044935)
  23. #
  24. # Quite interesting. Interpolation up front is a kind of hedge, while
  25. # the block is either faster or slower for short strings. Long strings
  26. # clearly benefit the block
  27. #
  28. # ** assignment from array **
  29. # a, b = *array 0.080000 0.000000 0.080000 ( 0.080729)
  30. # a, *b = array 0.110000 0.000000 0.110000 ( 0.112028)
  31. # a, b = (shift, shift) 0.140000 0.000000 0.140000 ( 0.138396)
  32. # a = (shift); b = (shift) 0.100000 0.000000 0.100000 ( 0.099711)
  33. #
  34. # Very interesting. Inline assignments using splat are very fast (and)
  35. # incidentally MUCH faster on ruby 1.9. Shifts are not preferable.
  36. #
  37. # ** various operations **
  38. # !!obj 0.170000 0.000000 0.170000 ( 0.182985)
  39. # obj ? true : false 0.150000 0.000000 0.150000 ( 0.156731)
  40. # if obj then else end 0.160000 0.000000 0.160000 ( 0.161405)
  41. #
  42. # Demonstrates the full syntax is slightly faster.
  43. #
  44. # ** method signatures **
  45. # method call 0.300000 0.000000 0.300000 ( 0.300031)
  46. # method with block 0.360000 0.000000 0.360000 ( 0.370484)
  47. # method with &block 0.440000 0.000000 0.440000 ( 0.438172)
  48. # bloc call 0.330000 0.000000 0.330000 ( 0.328342)
  49. # bloc with block 2.790000 0.470000 3.260000 ( 3.272963)
  50. # bloc with &block 0.450000 0.000000 0.450000 ( 0.453583)
  51. #
  52. # Note it is very expensive to turn a block into a proc
  53. Benchmark.bm(25) do |x|
  54. puts "** short strings **"
  55.  
  56. n = 100000
  57. str = "interpolation"
  58.  
  59. def m_str(input, check)
  60. check ? true : false
  61. end
  62.  
  63. x.report "m(\"string\", true)" do
  64. n.times { m_str("string #{str}", true) }
  65. end
  66.  
  67. x.report "m(\"string\", false)" do
  68. n.times { m_str("string #{str}", false) }
  69. end
  70.  
  71. def m_yield(check)
  72. check ? yield : false
  73. end
  74.  
  75. x.report "m(true) # :yield: " do
  76. n.times { m_yield(true) { "string #{str}" } }
  77. end
  78.  
  79. x.report "m(false)" do
  80. n.times { m_yield(false) { "string #{str}" } }
  81. end
  82.  
  83. puts
  84. puts "** long strings **"
  85.  
  86. x.report "m(\"string\", true)" do
  87. n.times { m_str("string #{str} string #{str} string #{str} string #{str} string #{str}", true) }
  88. end
  89.  
  90. x.report "m(\"string\", false)" do
  91. n.times { m_str("string #{str} string #{str} string #{str} string #{str} string #{str}", false) }
  92. end
  93.  
  94. x.report "m(true) # :yield: " do
  95. n.times { m_yield(true) { "string #{str} string #{str} string #{str} string #{str} string #{str}" } }
  96. end
  97.  
  98. x.report "m(false)" do
  99. n.times { m_yield(false) { "string #{str} string #{str} string #{str} string #{str} string #{str}" } }
  100. end
  101.  
  102. puts
  103. puts "** assignment from array **"
  104.  
  105. x.report "a, b = *array" do
  106. n.times {
  107. array = [1,2,3]
  108. a, b = *array
  109. }
  110. end
  111.  
  112. x.report "a, *b = array" do
  113. n.times {
  114. array = [1,2,3]
  115. a, *b = array
  116. }
  117. end
  118.  
  119. x.report "a, b = (shift, shift)" do
  120. n.times {
  121. array = [1,2,3]
  122. a, b = array.shift, array.shift
  123. }
  124. end
  125.  
  126. x.report "a = (shift); b = (shift)" do
  127. n.times {
  128. array = [1,2,3]
  129. a = array.shift
  130. b = array.shift
  131. }
  132. end
  133.  
  134. x.report "a, b, c, d, e = *array" do
  135. n.times {
  136. array = [1,2,3,4,5]
  137. a, b, c, d, e = *array
  138. }
  139. end
  140.  
  141. x.report "a = (shift); ..." do
  142. n.times {
  143. array = [1,2,3,4,5]
  144. a = array.shift
  145. b = array.shift
  146. c = array.shift
  147. d = array.shift
  148. e = array.shift
  149. }
  150. end
  151.  
  152. puts
  153. puts "** various operations **"
  154.  
  155. x.report "!!obj" do
  156. obj = Object.new
  157. (10 * n).times { !!obj }
  158. end
  159.  
  160. x.report "obj ? true : false" do
  161. obj = Object.new
  162. (10 * n).times { obj ? true : false }
  163. end
  164.  
  165. x.report "if obj then else end" do
  166. obj = Object.new
  167. (10 * n).times { if obj then true else false end }
  168. end
  169.  
  170. puts
  171. puts "** method signatures **"
  172.  
  173. class Sample
  174. def meth
  175. end
  176.  
  177. def bloc(&block)
  178. end
  179. end
  180.  
  181. x.report "method call" do
  182. obj = Sample.new
  183. (10 * n).times { obj.meth }
  184. end
  185.  
  186. x.report "method with block" do
  187. obj = Sample.new
  188. (10 * n).times { obj.meth {} }
  189. end
  190.  
  191. x.report "method with &block" do
  192. obj = Sample.new
  193. block = lambda {}
  194. (10 * n).times { obj.meth(&block) }
  195. end
  196.  
  197. x.report "bloc call" do
  198. obj = Sample.new
  199. (10 * n).times { obj.bloc }
  200. end
  201.  
  202. x.report "bloc with block" do
  203. obj = Sample.new
  204. (10 * n).times { obj.bloc {} }
  205. end
  206.  
  207. x.report "bloc with &block" do
  208. obj = Sample.new
  209. block = lambda {}
  210. (10 * n).times { obj.bloc(&block) }
  211. end
  212. end
Add Comment
Please, Sign In to add comment