From: Alexander Schofield Date: 2002-02-08T06:24:15+09:00 Subject: Re: Why is one slower than the other? 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 > } > > 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