From 07bf56f2fb6fa529d271ed69bdb67e53b92eedcf Mon Sep 17 00:00:00 2001 From: Watson Date: Mon, 18 Nov 2013 22:34:01 +0900 Subject: [PATCH] add a benchmark for string concatenation because CRuby has special optimization for short string --- test/Benchmark/app/benchmark/string/concat.rb | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/Benchmark/app/benchmark/string/concat.rb b/test/Benchmark/app/benchmark/string/concat.rb index 4f35497a..5afd75b9 100644 --- a/test/Benchmark/app/benchmark/string/concat.rb +++ b/test/Benchmark/app/benchmark/string/concat.rb @@ -1,6 +1,7 @@ def string_concat(x) ascii = "hello" utf8 = "こんにちは" + long_string = "The quick brown fox jumps over the lazy dog." x.report "interpolation with ASCII" do 100000.times do @@ -14,6 +15,12 @@ def string_concat(x) end end + x.report "interpolation with long" do + 100000.times do + "#{long_string} #{long_string} #{long_string}" + end + end + x.report "+ with ASCII" do 100000.times do ascii + ascii + ascii @@ -26,6 +33,12 @@ def string_concat(x) end end + x.report "+ with long" do + 100000.times do + long_string + long_string + long_string + end + end + x.report "<< with ASCII" do 100000.times do str = ascii.dup @@ -40,6 +53,13 @@ def string_concat(x) end end + x.report "<< with long" do + 100000.times do + str = long_string.dup + str << str << str + end + end + x.report "concat with ASCII" do 100000.times do str = ascii.dup @@ -54,6 +74,13 @@ def string_concat(x) end end + x.report "concat with long" do + 100000.times do + str = long_string.dup + str.concat(str).concat(str) + end + end + x.report "* with ASCII" do 100000.times do ascii * 10 @@ -65,4 +92,10 @@ def string_concat(x) utf8 * 10 end end + + x.report "* with long" do + 100000.times do + long_string * 10 + end + end end