From: Robert Klemme Date: 2005-08-05T03:11:06+09:00 Subject: Re: Defining regexp's and variables set by them Robert Klemme wrote: > Garance A Drosehn wrote: >> On 8/2/05, Caleb Clausen wrote: >>> On 8/1/05, Garance A Drosehn wrote: >>>> Does ruby keep the compiled-code for a method after >>>> the method is finished? >>> >>> Uhhh, it's nowhere near that fancy. Ruby is a fairly traditional >>> interpreter, without even bytecode compilation. That's why it's >>> so slow. >> >> That is what I expected. So I'm back to wishing to have one >> method which creates all the Regexp.new's as @@variables, >> and then I can reference those compiled Regexp's in other >> methods for that class. > > Performance does not differ much between a regexp in place and a > regexp compiled once and stored in a variable or constant (assuming no > interpolation is used or interpolation with "o" is used - otherwise > both scenarios have different semantics anyway and can't be > compared). Ruby *has* been optimized to make in place regexps > efficient - there is no recompilation of the regexp on every pass. > > You can try it out with the attached script. Using several > invocations either of the two is faster > > user system total real > direct 0.312000 0.000000 0.312000 ( 0.305000) > compiled 0.313000 0.000000 0.313000 ( 0.306000) > > user system total real > direct 0.313000 0.000000 0.313000 ( 0.319000) > compiled 0.297000 0.000000 0.297000 ( 0.305000) > >> Almost. I was saying that I *wanted* to have them distant, >> because the result is more readable (for what I'm doing, IMO), >> and for the efficiency benefit. > > As I said there is no such thing as an efficiency benefit in using > "remote" regexps. > >> This may seem weird, but most >> of the regexp's that I'm talking about are three or four full lines >> long, complete with a few regexp tricks that take a few more >> lines of comments to explain what the regexp is doing. The >> case-statement is *much* more readable if the regexp's are >> separated from the case statement. > > I'd stick with the readability argument and forget about the > performance here. The question is, does the code become more > readable by moving the regexps out of the case statement? I don't > know your code but I'd say it's not automatically so. > > Kind regards > > robert Sorry, I forgot the attachment. Here's the script: robert require 'benchmark' REPEAT = 10000 RX = /foo/ TEXT = <