From: Charles Oliver Nutter Date: 2011-12-19T08:00:55+09:00 Subject: Re: perf optimization using profile results On Thu, Dec 15, 2011 at 5:30 PM, Eric Wong wrote: > Chuck Remes wrote: >> 6. In hot code when looking up a value in an array, it's a tad faster >> to use #at instead of #[]. The reason is that #[] can take different >> arguments other than a simple index (like a Range). Those extra >> argument checks can add up. > >> These are all micro-optimizations and may not even be valid on your >> runtime of choice. > > Exactly. > > Array#at shouldn't be faster than Array#[] on MRI 1.9+ with a single arg > (ref: insns.def). Indeed. Array#[] is slower on JRuby because String#[] uses backref ($~), and since we can't tell at compile time whether we're calling Array#[] or String#[], we always prepare space for backref. There's no #at method that uses backref, so we can elide the allocation of space for backref. I plan to reduce the cost of backref allocation, but it's not there yet. In any case, JRuby's quite fast either way. - Charlie