Guest User

Untitled

a guest
Oct 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. require 'benchmark'
  3. class Test
  4.  
  5. CONST = 5
  6. N = 1_000_000
  7.  
  8. def test_const
  9. Benchmark.bm 20 do |results|
  10. results.report 'one' do
  11. N.times { CONST }
  12. end
  13. results.report "two" do
  14. N.times { self.class::CONST}
  15. end
  16. end
  17. end
  18.  
  19. def test_string_insert
  20. Benchmark.bm 20 do |results|
  21. x = 'my'
  22. results.report 'one' do
  23. N.times { s = "data"; s.insert(0, x) }
  24. end
  25. results.report "two" do
  26. N.times { s = "data"; s = "#{x}#{s}" }
  27. end
  28. end
  29. end
  30.  
  31. # one/two simliar, three slower
  32. def test_string_concat
  33. Benchmark.bm 20 do |results|
  34. x = 'my' * 100
  35. results.report 'one' do
  36. N.times { "data#{x}" }
  37. end
  38. results.report "two" do
  39. N.times { "data" << x }
  40. end
  41. results.report 'three' do
  42. N.times { "data" + x }
  43. end
  44. end
  45. end
  46.  
  47. # similar performance
  48. def test_assignment
  49. Benchmark.bm 20 do |results|
  50. results.report 'one' do
  51. N.times { x = {}; x[5] = 6; x[7] = 8 }
  52. end
  53. results.report "two" do
  54. N.times { (x ||= {})[5] = 6; (x ||= {})[7] = 8 }
  55. end
  56. end
  57. end
  58.  
  59. # getch about 35% faster
  60. def test_strscan_getch
  61. require 'strscan'
  62. y = StringScanner.new("hallöchen")
  63. Benchmark.bm 20 do |results|
  64. results.report 'one' do
  65. N.times { y.reset; 8.times {y.scan(/./)} }
  66. end
  67. results.report "two" do
  68. N.times { y.reset; 8.times {y.getch} }
  69. end
  70. end
  71. end
  72.  
  73. # rest about 55% (47%) faster (with setting position to eos)
  74. def test_strscan_rest
  75. require 'strscan'
  76. y = StringScanner.new("hallöchen\nsome\nother data")
  77. Benchmark.bm 20 do |results|
  78. results.report 'one' do
  79. N.times { y.pos = 4; y.scan(/.*/m) }
  80. end
  81. results.report "two" do
  82. N.times { y.pos = 4; y.rest; y.terminate }
  83. end
  84. end
  85. end
  86.  
  87. INDENT = /^(?:\t| {4})/
  88.  
  89. # two about 60% faster
  90. def test_string_gsub
  91. y = " this is some string\n with some data\n that continues here"
  92. Benchmark.bm 20 do |results|
  93. results.report 'one' do
  94. N.times { y.gsub(/\n( {0,3}\S)/, ' \\1').gsub!(INDENT, '') }
  95. end
  96. results.report "two" do
  97. N.times { x = y; x.gsub!(/\n( {0,3}\S)/, ' \\1'); x.gsub!(INDENT, '') }
  98. end
  99. end
  100. end
  101.  
  102. # two about 35% faster (using ! methods and using normal methods)
  103. def test_string_gsub_vs_tr
  104. y = "123. hallo ich habe! <span>hiere</span>"
  105. Benchmark.bm 20 do |results|
  106. results.report 'one' do
  107. N.times { x = y.dup; x.gsub!(/^[^a-zA-Z]+|[^a-zA-Z0-9 -]+/, ''); x.gsub!(' ', '-') }
  108. end
  109. results.report "two" do
  110. N.times { x = y.dup; x.gsub!(/^[^a-zA-Z]+/, ''); x.tr!('^a-zA-Z0-9 -', ''); x.tr!(' ', '-')}
  111. end
  112. end
  113. end
  114.  
  115. def invoke_me
  116. end
  117.  
  118. # two about 50% faster (using either hash line)
  119. def test_invoke_method
  120. me = :me
  121. hash = Hash.new {|h,k| h[k] = "invoke_#{k}"}
  122. #hash = {:me => "invoke_me"}
  123. Benchmark.bm 20 do |results|
  124. results.report 'one' do
  125. N.times { send("invoke_#{me}") }
  126. end
  127. results.report "two" do
  128. N.times { send(hash[me]) }
  129. end
  130. end
  131. end
  132.  
  133. # similar runtimes
  134. def test_string_times
  135. Benchmark.bm 20 do |results|
  136. results.report 'one' do
  137. N.times { indent = 2; indent += 2; ' '*indent }
  138. end
  139. results.report "two" do
  140. N.times { indent = ' '; indent += indent; indent }
  141. end
  142. end
  143. end
  144.  
  145. # runtimes are equal
  146. def test_string_concat2
  147. Benchmark.bm 20 do |results|
  148. x = 'my'
  149. results.report 'one' do
  150. N.times { "data#{x}>#{x}\n" }
  151. end
  152. results.report "two" do
  153. N.times { "data" << x << '>' << x << "\n" }
  154. end
  155. end
  156. end
  157.  
  158. # simliar runtimes
  159. def test_regexp_match
  160. Benchmark.bm 20 do |results|
  161. x = "my some thing is\nasdfasd fsad fasdf adsf \ndsaf adfadsfsa\n"
  162. results.report 'one' do
  163. N.times { x =~ /\n\Z/ }
  164. end
  165. results.report "two" do
  166. N.times { x[-1,1] == "\n" }
  167. end
  168. end
  169. end
  170.  
  171. # two 50% faster
  172. def test_equal_vs_regexp_match
  173. Benchmark.bm 20 do |results|
  174. x = "script"
  175. results.report 'one' do
  176. N.times { x =~ /^script$/ }
  177. end
  178. results.report "two" do
  179. N.times { x == 'script' }
  180. end
  181. end
  182. end
  183.  
  184. # two 50% faster
  185. def test_equal_vs_regexp_match2
  186. Benchmark.bm 20 do |results|
  187. x = "script"
  188. re = /^script$/
  189. results.report 'one' do
  190. N.times { x =~ re }
  191. end
  192. results.report "two" do
  193. N.times { x == 'script' }
  194. end
  195. end
  196. end
  197.  
  198. attr_accessor :accessor_test_1
  199. def accessor_test_2
  200. @accessor_test_2
  201. end
  202.  
  203. # one 33% faster on Ruby 1.8.7, 6% on 1.9.2
  204. def test_accessors
  205. @accessor_test_1 = 1
  206. @accessor_test_2 = 1
  207. Benchmark.bm do |results|
  208. results.report 'one' do
  209. N.times { accessor_test_1 }
  210. end
  211. results.report "two" do
  212. N.times { accessor_test_2 }
  213. end
  214. end
  215. end
  216.  
  217.  
  218. attr_accessor :assign_test_1
  219. def assign_test_3
  220. @assign_test_1 ||= []
  221. end
  222. def assign_test_2
  223. @assign_test_2 ||= []
  224. #only useful when done on few objects on methods that are accessed often
  225. class << self; attr_accessor :assign_test_2; end
  226. end
  227.  
  228. # one 40% faster when two=assign_test_3, equal when two=assign_test_2
  229. def test_accessors
  230. @assign_test_1 = []
  231. Benchmark.bm do |results|
  232. results.report 'one' do
  233. N.times { assign_test_1 }
  234. end
  235. results.report "two" do
  236. N.times { assign_test_2 }
  237. end
  238. end
  239. end
  240.  
  241.  
  242. # ary.select {|d| }.length vs ary.count
  243. def test_select_length_vs_count
  244. ary = []
  245. 1.upto(1000) {|i| ary << rand(100)}
  246. Benchmark.bm do |results|
  247. results.report 'one' do
  248. N.times { ary.count {|c| c > 50} }
  249. end
  250. results.report "two" do
  251. N.times { ary.select {|c| c > 50}.length }
  252. end
  253. end
  254. end
  255.  
  256.  
  257. #
  258. def test_unpack_vs_ord
  259. string = "abcd".force_encoding('ASCII-8BIT')
  260. x = nil
  261. Benchmark.bm do |results|
  262. results.report 'one' do
  263. N.times { x = string.unpack('C4'); (x[0] << 21) + (x[1] << 14) + (x[2] << 7) + x[3] }
  264. end
  265. results.report "two" do
  266. N.times { (string[0].ord << 21) + (string[1].ord << 14) + (string[2].ord << 7) + string[3].ord }
  267. end
  268. end
  269. end
  270.  
  271. def test_camelize
  272. s = "my_underscore"
  273. Benchmark.bm do |results|
  274. re1 = /^(\w)|_(\w)/
  275. results.report 'one' do
  276. N.times { x = s.dup; x.gsub!(re1) { ($1 || $2).upcase } }
  277. end
  278. results.report "two" do
  279. N.times { x = s.dup; x.split('_').inject(''){ |s,x| s << x[0..0].upcase + x[1..-1] } }
  280. end
  281. re2 = /_/
  282. results.report "three" do
  283. N.times { x = s.dup; x.split(re2).inject(''){ |s,x| s << x[0..0].upcase + x[1..-1] } }
  284. end
  285. end
  286. end
  287.  
  288. # two about 20% faster on 1.8.7, 8% faster on 1.9.3
  289. def test_regexp_creation
  290. Benchmark.bm 20 do |results|
  291. x = "script"
  292. re = /^script$/
  293. results.report 'one' do
  294. N.times { x =~ re }
  295. end
  296. results.report "two" do
  297. N.times { x =~ /^script$/ }
  298. end
  299. end
  300. end
  301.  
  302. end
  303.  
  304. # Change this to run a benchmark
  305. Test.new.test_regexp_creation
Add Comment
Please, Sign In to add comment