Project

General

Profile

Actions

Bug #6525

closed

misleading example in Permformance section of Regexp documentation?

Added by nathanst (Nathan Stratton Treadway) almost 12 years ago. Updated almost 12 years ago.

Status:
Closed
Target version:
-
ruby -v:
1.9.3
Backport:
[ruby-core:45359]

Description

In the Performance section of the Regexp documention, it is stated that changing from Regexp.new('a?' * 29 + 'a' * 29) to Regexp.new('\A' 'a?' * 29 + 'a' * 29) speeds up the match significantly. However, '\A' 'a?' * 29 actually expands to "\Aa?\Aa?....\Aa?" -- which doesn't seem like it's what you'd actually want, since [for example] Regexp.new('\A' 'a?' * 29 + 'a' * 29).match('a' * 50) will only match 30 'a' characters [the 29 from 'a' * 29, plus one single one from the set of '\Aa?' items. [That is, Regexp.new('\A' 'a?' * 29 + ...) is doesn't match any more 'a' characters than Regexp.new('\A' 'a?' + ...) would.]

One can get around this by changing the expression to Regexp.new('\A' + 'a?' * 29 + 'a' * 29), in which case additional "a" characters do get matched as the string gets longer... but with that Regexp the .match('a' * 29) still takes about the same amount of time as the original one.

So I wonder if there is some better example that could be used here?

Nathan

Actions

Also available in: Atom PDF

Like0
Like0Like0