add a benchmark for string concatenation because CRuby has special optimization for short string

This commit is contained in:
Watson
2013-11-18 22:34:01 +09:00
parent ab7f8a3cc9
commit 07bf56f2fb

View File

@@ -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