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% --
In reply to Re^4: Combining regexes
by BrowserUk
in thread Combining regexes
by davies
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |