lib/benchmark/gsub.rb in niceql-0.1.23 vs lib/benchmark/gsub.rb in niceql-0.1.24

- old
+ new

@@ -1,25 +1,34 @@ require 'benchmark/ips' -$str = File.open( './txt' ).readlines.join(' ') +# 2 + 1 = 3 object +def slow_plus + 'foo' + 'bar' +end # 2 + 1 = 3 object -def _gsub(str) - str.gsub( /\s+/, '' ) +def slow_concat + 'foo'.concat 'bar' end # 2 + 1 = 3 object -def _gsub!(str) - str.gsub!( /\s+/, '' ) +def slow_append + 'foo' << 'bar' end +# 1 object +def fast + 'foo' 'bar' +end -def test_gsubs( word_count, string_count = 1000 ) - arr = Array.new( string_count ) { $str.split.sample(word_count).join(' ') } - - Benchmark.ips do |x| - - x.report('_gsub!') { arr.each{ |str| _gsub!(str) } } - x.report('_gsub') { arr.each{ |str| _gsub(str) } } - x.compare! - end +def fast_interpolation + "#{'foo'}#{'bar'}" end + +Benchmark.ips do |x| + x.report('String#+') { slow_plus } + x.report('String#concat') { slow_concat } + x.report('String#append') { slow_append } + x.report('"foo" "bar"') { fast } + x.report('"#{\'foo\'}#{\'bar\'}"') { fast_interpolation } + x.compare! +end \ No newline at end of file