From: Ryan Davis Date: 2002-09-21T05:18:10+09:00 Subject: Re: Speed up suggestions (regex obj vs plain usage) On Friday, September 20, 2002, at 12:54 PM, Ryan Davis wrote: > outside the loop: > > comment_re = /^#/ > > inside the loop: > > next if line =~ comment_re > > if line =~ /[^:]+:([^:]+):/ then > pom = $1 > if pom =~ r > outf.write( line + "\n" ) WOW! I just found something out that is pretty interesting... too bad I don't understand why yet... By sheer luck I noticed in my email that I didn't extract the above regex to be instantiated outside the loop. So I did it. And it slowed down, a lot! here's the profile: BEFORE: time = 0.03045900 sec % cumulative self self total time seconds seconds calls ms/call ms/call name 50.00 0.01 0.01 1 7.81 15.62 IO#each_line 50.00 0.02 0.01 86 0.09 0.09 String#=~ AFTER: time = 0.04492900 sec % cumulative self self total time seconds seconds calls ms/call ms/call name 66.67 0.03 0.03 1 31.25 46.88 IO#each_line 33.33 0.05 0.02 127 0.12 0.12 String#=~ So I made a new version and got rid of all the regex instances (diff follows) and the profile looks like: time = 0.00842100 sec % cumulative self self total time seconds seconds calls ms/call ms/call name 100.00 0.01 0.01 1 7.81 7.81 IO#each_line (we are now so small and fast (file sould be bigger for better sampling) that the profile has no relevant data... subsequent runs show the sampling jumping all over but the total time is the same) <528> diff parse[34].rb 4,6c4,6 < r = /xxx\\xxxxxxxx/i < second_re = /[^:]+:([^:]+):/ < comment_re = /^#/ --- > > > 10c10 < next if line =~ comment_re --- > next if line =~ /^#/ 12c12 < if line =~ second_re then --- > if line =~ /[^:]+:([^:]+):/ then 14c14 < if pom =~ r --- > if pom =~ /xxx\\xxxxxxxx/i NOW, can someone tell me why regexen in place are much faster than instantiated regexen?