PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/benchmark/core/string/bench_sub.rb

http://github.com/rubinius/rubinius
Ruby | 114 lines | 108 code | 5 blank | 1 comment | 4 complexity | 693ab299b2ba2d56aa128f1e6998c156 MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, 0BSD, GPL-2.0, LGPL-2.1
  1. require 'benchmark'
  2. require 'benchmark/ips'
  3. Benchmark.ips do |x|
  4. string = "2011-Mar-08T10:09:08.467"
  5. # para_string is 2000 characters, containing only 11 digits (in 3 \d+ matches)
  6. para_string = "What is Lorem Ipsum?\n" +
  7. "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\n" +
  8. "Why do we use it?\n" +
  9. "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).\n" +
  10. "Where can I get some?\n" +
  11. "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc."
  12. x.report "gsub" do |times|
  13. i = 0
  14. while i < times
  15. string.gsub(/[^-+',.\/:@[:alnum:]\[\]\x80-\xff]+/n, ' ')
  16. i += 1
  17. end
  18. end
  19. x.report "gsub!" do |times|
  20. i = 0
  21. while i < times
  22. string.gsub!(/[^-+',.\/:@[:alnum:]\[\]\x80-\xff]+/n, ' ')
  23. i += 1
  24. end
  25. end
  26. x.report "gsub single character" do |times|
  27. i = 0
  28. while i < times
  29. para_string.gsub(/./, 'X')
  30. i += 1
  31. end
  32. end
  33. x.report "gsub! single character" do |times|
  34. i = 0
  35. para_string2 = para_string.dup
  36. while i < times
  37. para_string2.gsub(/./, 'X')
  38. i += 1
  39. end
  40. end
  41. x.report "gsub to shorter size" do |times|
  42. i = 0
  43. while i < times
  44. para_string.gsub(/\w{9,}/, 'X')
  45. i += 1
  46. end
  47. end
  48. x.report "gsub! to shorter size" do |times|
  49. i = 0
  50. para_string2 = para_string.dup
  51. while i < times
  52. para_string2.gsub!(/\w{9,}/, 'X')
  53. i += 1
  54. end
  55. end
  56. x.report "gsub to longer size" do |times|
  57. i = 0
  58. while i < times
  59. para_string.gsub(/\w{9,}/, '\0and9chars')
  60. i += 1
  61. end
  62. end
  63. x.report "gsub! to longer size" do |times|
  64. i = 0
  65. para_string2 = para_string.dup
  66. while i < times
  67. para_string2.gsub!(/\w{9,}/, '\0and9chars')
  68. i += 1
  69. end
  70. end
  71. x.report "sparse gsub" do |times|
  72. i = 0
  73. while i < times
  74. para_string.gsub(/\d+/, '#')
  75. i += 1
  76. end
  77. end
  78. x.report "sparse gsub!" do |times|
  79. i = 0
  80. para_string2 = para_string.dup
  81. while i < times
  82. para_string2.gsub!(/\d+/, '#')
  83. i += 1
  84. end
  85. end
  86. x.report "gsub, giving a block" do |times|
  87. i = 0
  88. while i < times
  89. para_string.gsub(/\w+/) { |m| m+m.size.to_s }
  90. i += 1
  91. end
  92. end
  93. x.report "gsub!, giving a block" do |times|
  94. i = 0
  95. para_string2 = para_string.dup
  96. while i < times
  97. para_string2.gsub!(/\w+/) { |m| m+m.size.to_s }
  98. i += 1
  99. end
  100. end
  101. end