From: Alan Chen Date: 2002-02-08T09:11:40+09:00 Subject: Re: Why is one slower than the other? On Fri, Feb 08, 2002 at 06:24:15AM +0900, Alexander Schofield wrote: > In B you're instantiating a new Regexp on each iteration. > > Kirk Haines wrote: > > > > Program A: > > > > z = 0 > > t = "0123456789abcdefghijklmnopqrstuvwxyz" > > (0..999999).each { > > z += 1 if t =~ /abc/ > > } > > > > Program B: > > > > z = 0 > > t = "0123456789abcdefghijklmnopqrstuvwxyz" > > rexp = Regexp.new('abc'); > > (0..999999).each { > > z += 1 if rexp.match t you might try replacing this with z +=1 if( rexp =~ t ) to get a better comparison > > } > > > > B has a runtime that is about 1.7 times A. Why? This is on Ruby 1.6.6 on > > a Linux box. > > > > Kirk Haines > > > > ------------------------------------------------------------------------ > > Name: > > Type: unspecified type (application/octet-stream) > > Encoding: base64 > > -- > Alexander Schofield > -- Alan Chen Digikata LLC http://digikata.com