Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add perf_string.rb to do benchmark of string

  • Loading branch information...
commit cbbeab8c29730c7f315778c16fde8fd27da7dddd 1 parent 677f704
@Watson1978 Watson1978 authored
Showing with 157 additions and 0 deletions.
  1. +157 −0 perf/perf_string.rb
  2. 0  perf/run.rb
View
157 perf/perf_string.rb
@@ -0,0 +1,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
View
0  perf/run.rb 100644 → 100755
File mode changed
Please sign in to comment.
Something went wrong with that request. Please try again.