From: "Mauricio Fernández" Date: 2002-12-11T18:52:14+09:00 Subject: Re: replacing chars in string On Wed, Dec 11, 2002 at 03:18:05PM +0900, nobu.nokada@softhome.net wrote: > Hi, > > At Wed, 11 Dec 2002 14:58:28 +0900, > why the lucky stiff wrote: > > n = 1000 > > str = "count=(a+b); if(a<=b) d[i]=10;f(a,b)" > > test = nil > > bm do |x| > > x.report( "chained gsubs:" ) do > > n.times do > > test = str.gsub(/,/,' , ').gsub(/\(/,' ( ').gsub(/\)/,' ) ').gsub(/([^<>])=/,'\1 = ').gsub(/;/,";\n") > > end > > # puts > > # p test > > end > x.report( "chained gsubs 2:" ) do > n.times do > test = str.gsub(/[,()]/,' \& ').gsub(/([^<>])=/,'\1 = ').gsub(/;/,";\n") > end > # puts > # p test > end > > With 10,000 iterations: > > user system total real > chained gsubs: 2.270000 0.020000 2.290000 ( 2.306303) > chained gsubs 2: 2.030000 0.040000 2.070000 ( 2.066607) > grouped gsub: 5.570000 0.060000 5.630000 ( 5.758734) > gsub and case: 3.720000 0.070000 3.790000 ( 3.950212) > x.report( "chained gsubs 3:" ) do n.times do test = str.gsub(/[,()]/,' \& ').gsub!(/([^<>])=/,'\1 = ').gsub!(/;/,";\n") end # puts # p test end :) With 100,000 iterations: user system total real chained gsubs: 6.270000 0.020000 6.290000 ( 6.389039) chained gsubs 2: 5.760000 0.020000 5.780000 ( 5.808392) chained gsubs 3: 5.620000 0.000000 5.620000 ( 5.703204) grouped gsub: 19.880000 0.020000 19.900000 ( 20.185286) gsub and case: 12.580000 0.020000 12.600000 ( 12.791412) -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com /* * Please skip to the bottom of this file if you ate lunch recently * -- Alan */ -- from Linux kernel pre-2.1.91-1