in reply to Re^3: Combining regexes
in thread Combining regexes

If you're operating on a single string of a few dozen/hundred/thousand characters, the difference between s///g and tr/// will not be detectable.

Sorry, but that simply isn't true.

For a 3 character string tr/// takes 1/3rd the time of starting up the regex engine.

For a 30 character string, it is 1/8th the time.

And by the time you get to just 300 characters, the regex engine is already 20 times slower for this task.

$n = 1e0; cmpthese -1,{ a => q[ my $s='axb'; $s x= $n; $s=~tr[x][y]; ], b => q[ my $s='axb'; $s x= $n; $s=~s[x][y]g; ] };; Rate b a b 1000796/s -- -60% a 2493375/s 149% -- $n = 1e1; cmpthese -1,{ a => q[ my $s='axb'; $s x= $n; $s=~tr[x][y]; ], b => q[ my $s='axb'; $s x= $n; $s=~s[x][y]g; ] };; Rate b a b 234727/s -- -87% a 1838169/s 683% -- $n = 1e2; cmpthese -1,{ a => q[ my $s='axb'; $s x= $n; $s=~tr[x][y]; ], b => q[ my $s='axb'; $s x= $n; $s=~s[x][y]g; ] };; Rate b a b 28926/s -- -95% a 585631/s 1925% -- $n = 1e3; cmpthese -1,{ a => q[ my $s='axb'; $s x= $n; $s=~tr[x][y];], b => q[ my $s='axb'; $s x= $n; $s=~s[x][y]g;] };; Rate b a b 3087/s -- -96% a 76540/s 2379% --

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^5: Combining regexes
by AnomalousMonk (Archbishop) on May 21, 2016 at 17:58 UTC
    ... will not be detectable.

    I should have said "will not be noticeable in practice." In fact, I will say it...


    Give a man a fish:  <%-{-{-{-<

      I should have said "will not be noticeable in practice." In fact, I will say it...

      Agreed. But...

      Since there is no practical, clarity, nor conciseness advantage for this operation; why would you choose to use the less efficient over the more?

      Better methinks to get into the habit of thinking: 1 for 1 replacement of single chars, use tr///; than to have to always make a decision (or even investigate at what point) you should shift tack for this maneuverer.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Agreed. But... um...   Well, I can't really come up with any pertinent exception or counterargument here. I just wanted to make the point to davies that in the context of the OPed question, there wasn't much difference.


        Give a man a fish:  <%-{-{-{-<