in reply to SUBSTR OR REGEX: WHICH WILL YOU FAVOUR?

substr is always faster than a regex. index is always faster than a regex. Combining the 2, they are faster than a regex. But if you add an if else block or more than 1 index, they start being the same speed as a regex. Regex parsing logic is faster than Perl optree logic. But a regex can never beat memcpy (AKA substr) and strstr (AKA index).
  • Comment on Re: SUBSTR OR REGEX: WHICH WILL YOU FAVOUR?

Replies are listed 'Best First'.
Re^2: SUBSTR OR REGEX: WHICH WILL YOU FAVOUR?
by vsespb (Chaplain) on Oct 26, 2013 at 22:11 UTC
    Recently I found simple regexp $x =~ /Something/ is just 5%-10% slower than index (on small inputs)

    I actually can't see reason why perl won't recognize /Something/ as something that can be replaced by index() at compile time

    And 10% is just implementation overhead.