From: Jeremy Bopp Date: 2012-02-03T13:08:12+09:00 Subject: Re: Basic Ruby performance On 02/02/2012 09:47 PM, Josh Cheek wrote: > On Thu, Feb 2, 2012 at 8:57 PM, Jeremy Bopp wrote: > >> >> Try a similar change to the other looping variations that have been >> discussed and see if and how much they may improve. For me I didn't >> really see any difference between using re as above or using the simple >> regexp directly; however, the code was almost an order of magnitude >> slower when I replaced the comparison as follows: >> >> s =~ / test#{} / >> >> It seems that Ruby is smart enough to see that the simple regexp will >> never need to be re-evaluated. The regexp used above must force that >> optimization off because #{} while constantly evaluated to the empty >> string is technically dynamic, thus the regexp needs to be re-evaluated >> in every iteration of the loop. >> >> > The o flag tells Ruby to only interpolate the first time, and then cache > the regex > > s =~ / test#{} /o Thanks for the tip! :-) Still, sprinkling that option around might cause more confusion than assigning the regexp to a constant and using that reference instead. Someone reading your code later may overlook the dangling "o" on a complex regexp and be left wondering why the interpolation isn't happening like they expect. I guess it really depends on the scope of your task though. -Jeremy