Basic Ruby performance

Thanks for the tip! :slight_smile: 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

ยทยทยท

On 02/02/2012 09:47 PM, Josh Cheek wrote:

On Thu, Feb 2, 2012 at 8:57 PM, Jeremy Bopp <jeremy@bopp.net> 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

Do you realize that there are multiple Ruby interpreters with
different implementations and strengths? Have you tried your code with
one of the others? The two other major ones are:

JRuby -- http://jruby.org/
Rubinius -- http://rubini.us/

And a great tool for easily switching between Ruby runtimes:

RVM -- http://beginrescueend.com/

Kirk Haines

ยทยทยท

On Fri, Feb 3, 2012 at 9:40 AM, Dmitry Nikiforov <dniq@dniq-online.com> wrote:

I guess I will have to wait and see if Ruby interpreter becomes more
efficient :frowning: But I have to confess: I'm REALLY tempted to, in some
cases, forgo the performance in favor of handsome code :slight_smile:

Kirk Haines wrote in post #1043963:

Do you realize that there are multiple Ruby interpreters with
different implementations and strengths? Have you tried your code with
one of the others? The two other major ones are:

JRuby -- http://jruby.org/
Rubinius -- http://rubini.us/

Yeah, I saw it in another post - I'll give them a try.

Thanks! :slight_smile:

ยทยทยท

--
Posted via http://www.ruby-forum.com/\.