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% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Combining regexes
by AnomalousMonk (Archbishop) on May 21, 2016 at 17:58 UTC | |
by BrowserUk (Patriarch) on May 21, 2016 at 18:05 UTC | |
by AnomalousMonk (Archbishop) on May 21, 2016 at 18:47 UTC | |
by Anonymous Monk on May 22, 2016 at 00:43 UTC |