require 'benchmark/ips' $str = File.open( './txt' ).readlines.join(' ') # 2 + 1 = 3 object def _gsub(str) str.gsub( /\s+/, '' ) end # 2 + 1 = 3 object def _gsub!(str) str.gsub!( /\s+/, '' ) 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 end