From: lewisd@...00f.net (Derek Lewis) Date: 2005-02-16T10:16:51+09:00 Subject: Curious regexp behavior On a whim, I just decided to try an experiment with regexps, to see how they perform in two slightly different cases. I wanted to see how using a single regexp object for many many evaluations performed compared to using the regexp within the loop. The scripts I wrote searched through a words file that is 234937 lines long. Here's the scripts I wrote, to clarify: 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}/ } } 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 } } 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? :) -- Derek Lewis =================================================================== Java Web-Application Developer Email : email@lewisd.com Cellular : 778.898.5825 Website : http://www.lewisd.com "If you've got a 5000-line JSP page that has "all in one" support for three input forms and four follow-up screens, all controlled by "if" statements in scriptlets, well ... please don't show it to me :-). Its almost dinner time, and I don't want to lose my appetite :-)." - Craig R. McClanahan