Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 157 lines (138 sloc) 2.146 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
str_ascii =<<EOS
Alice was beginning to get very tired of sitting by her sister on the
bank, and of having nothing to do: once or twice she had peeped into the
book her sister was reading, but it had no pictures or conversations in
it, 'and what is the use of a book,' thought Alice 'without pictures or
conversation?'
EOS

perf_test('new') do
  i = 0
  str = str_ascii.dup
  while i < 50_000
    String.new(str)
    i += 1
  end
end

perf_test('""') do
  i = 0
  str = str_ascii.dup
  while i < 50_000
    "{#{str_ascii}"
    i += 1
  end
end

perf_test('dup') do
  i = 0
  str = str_ascii.dup
  while i < 50_000
    str.dup
    i += 1
  end
end

perf_test('clone') do
  i = 0
  str = str_ascii.dup
  while i < 50_000
    str.clone
    i += 1
  end
end

perf_test('*') do
  i = 0
  str = str_ascii.dup
  while i < 500
    str * 1_000
    i += 1
  end
end

perf_test('+') do
  i = 0
  str1 = str_ascii.dup
  str2 = "0123456789"
  while i < 50_000
    str1 + str2
    i += 1
  end
end

perf_test('<<') do
  i = 0
  str1 = str_ascii.dup
  str2 = "0123456789"
  while i < 50_000
    str1 << str2
    i += 1
  end
end

perf_test('chomp!') do
  i = 0
  str = str_ascii.dup
  while i < 500
    str.chomp
    i += 1
  end
end

perf_test('[]') do
  i = 0
  str = str_ascii.dup
  while i < 50_000
    str[1..5]
    i += 1
  end
end

perf_test('[]=') do
  i = 0
  str = str_ascii.dup
  size = str.size
  while i < 50_000
    str[i%size] = "" << (i%255)
    i += 1
  end
end

perf_test('strip!') do
  i = 0
  str = str_ascii.dup
  while i < 50_000
    str.strip!
    i += 1
  end
end

perf_test('scan') do
  i = 0
  str = str_ascii.dup
  while i < 5_000
    str.scan(/\w+/)
    i += 1
  end
end


perf_test('split') do
  i = 0
  str = str_ascii.dup
  while i < 50_000
    str.split('\n')
    i += 1
  end
end

perf_test('gsub') do
  i = 0
  str = str_ascii.dup
  while i < 50_000
    str.gsub(/Alice/, "Rabbit")
    i += 1
  end
end


perf_test('reverse!') do
  i = 0
  str = str_ascii.dup
  while i < 50_000
    str.reverse!
    i += 1
  end
end


perf_test('=~') do
  i = 0
  str = str_ascii.dup
  while i < 50_000
    str =~ /Alice/
    i += 1
  end
end
Something went wrong with that request. Please try again.