From: Eric Hodel Date: 2005-02-16T14:29:31+09:00 Subject: Re: Curious regexp behavior --Apple-Mail-15--771973824 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed On 15 Feb 2005, at 17:16, Derek Lewis wrote: > First one: > > total = 0 > File.open( 'words', 'r' ) { |file| > file.each_line { |line| > word = line.chomp > total +=1 if word =~ /[a-df-h][aeiou]{2}/ ^^^^ inline regexp (part of the AST) > } > } > puts total > > Second one: > > rexp = /[a-df-h][aeiou]{2}/ > total = 0 > File.open( 'words', 'r' ) { |file| > file.each_line { |line| > word = line.chomp > total +=1 if word =~ rexp ^^^^ variable lookup > } > } > puts total > > > I expected the second one to be slightly faster, but was surprised to > see that it was actually slightly slower. I ran each one about 10-15 > times, and eyeballed an average. The results from each run after the > first were pretty consistant. > > It's just a curiosity, but does anyone know what might cause them to be > 'backwards' like that? :) Inline regexps are much faster than a variable lookup then using the methods on the Regexp object. -- Eric Hodel - drbrain@segment7.net - http://segment7.net FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 --Apple-Mail-15--771973824 content-type: application/pgp-signature; x-mac-type=70674453; name=PGP.sig content-description: This is a digitally signed message part content-disposition: inline; filename=PGP.sig content-transfer-encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFCEtoXMypVHHlsnwQRAh7aAJ46tcIOb0m2MDduBhOXkGpMgX5jTgCfYeqS Go0t7KlyH3HliLg7xAtWbQE= =aKt5 -----END PGP SIGNATURE----- --Apple-Mail-15--771973824--