# Basic Ruby performance

**URL:** <https://rubytalk.org/t/basic-ruby-performance/64768>\
**Category:** ruby-talk\
**Created:** [2 February 2012 22:20 UTC](https://rubytalk.org/t/basic-ruby-performance/64768 "2012-02-02T22:20:01Z")\
**Posts on this page:** 3\
**Page:** 3

<div class="post-metadata">

**Author:** ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)\
**Post date:** [3 February 2012 04:08 UTC](https://rubytalk.org/t/basic-ruby-performance/64768/41 "2012-02-03T04:08:12Z")

</div>

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

> **···**
>
> 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

---

<div class="post-metadata">

**Author:** ![Kirk\_Haines2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/kirk_haines2/32/1992_2.png) [@Kirk\_Haines2](https://rubytalk.org/u/Kirk_Haines2)\
**Post date:** [3 February 2012 17:21 UTC](https://rubytalk.org/t/basic-ruby-performance/64768/42 "2012-02-03T17:21:19Z")

</div>

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/](http://jruby.org/)  
Rubinius -- [http://rubini.us/](http://rubini.us/)

And a great tool for easily switching between Ruby runtimes:

RVM -- [http://beginrescueend.com/](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 ☹ But I have to confess: I'm REALLY tempted to, in some  
> > cases, forgo the performance in favor of handsome code 🙂

---

<div class="post-metadata">

**Author:** ![Dmitry\_Nikiforov](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@Dmitry\_Nikiforov](https://rubytalk.org/u/Dmitry_Nikiforov)\
**Post date:** [3 February 2012 17:36 UTC](https://rubytalk.org/t/basic-ruby-performance/64768/43 "2012-02-03T17:36:00Z")

</div>

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/](http://jruby.org/)  
> Rubinius -- [http://rubini.us/](http://rubini.us/)

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

Thanks! 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

[Previous page](https://rubytalk.org/t/basic-ruby-performance/64768.md?page=2)
