?Ruby 字符串连接
?
require 'benchmark' Benchmark.bm do |x| x.report('par') { s2 = '' 1000.times { |i| i = i.to_s; s2 = "#{s2}#{i}" } } x.report('par') { s1 = '' 1000.times { |i| i = i.to_s; s1 += i } } x.report('par') { s3 = '' 1000.times { |i| i = i.to_s; s3 = s3 << i } } end && 1 user system total real par 0.001524 0.000597 0.002121 ( 0.002119) par 0.001315 0.000667 0.001982 ( 0.001998) par 0.000478 0.000028 0.000506 ( 0.000512) user system total real par 0.002281 0.001376 0.003657 ( 0.003640) par 0.030634 0.010257 0.040891 ( 0.041087) par 0.000564 0.000002 0.000566 ( 0.000567)