in reply to Re^4: The indisputable speed of tr///
in thread The indisputable speed of tr///
His string eval was missing calls to quotemeta. I fixed this, added a few more solutions, and reran the benchmarks. They show that:
Without using eval (rot13dynre), your task can be accomplished in 2/3rd of the time it currently takes (rot13splitmap).
my %rot; @rot{'A'..'M'} = ('N'..'Z'); @rot{'N'..'Z'} = ('A'..'M'); @rot{'a'..'m'} = ('n'..'z'); @rot{'n'..'z'} = ('a'..'m'); my $chars_to_change = join('', map quotemeta, keys %rot); sub rot13dynre { local $_ = @_ ? $_[0] : $_; s/([$chars_to_change])/$rot{$1}/g; return $_; }
By using eval (rot13trbuilt), your task can be accomplished in 1/167th of the time it currently takes (rot13splitmap).
my %rot; @rot{'A'..'M'} = ('N'..'Z'); @rot{'N'..'Z'} = ('A'..'M'); @rot{'a'..'m'} = ('n'..'z'); @rot{'n'..'z'} = ('a'..'m'); *rot13builttr = eval 'sub { local $_ = @_ ? $_[0] : $_; tr{' . join('', map quotemeta, keys %rot) . '} {' . join('', map quotemeta, values %rot) . '}; return $_; }';
That should help convince your peers that this limited, well controlled, easily testable use of eval is appropriate here.
Benchmark results:
Benchmark: running rot13builtre, rot13builttr, rot13dynre, rot13dynre2 +, rot13splitmap, rot13substrfor, rot13tr, each for at least 3 CPU sec +onds... rot13builtre: 3 wallclock secs ( 3.13 usr + 0.00 sys = 3.13 CPU) +@ 572.75/s (n= 1795) rot13builttr: 4 wallclock secs ( 3.17 usr + 0.00 sys = 3.17 CPU) +@ 61539.53/s (n=195388) rot13dynre: 3 wallclock secs ( 3.07 usr + 0.00 sys = 3.07 CPU) +@ 567.80/s (n= 1746) rot13dynre2: 3 wallclock secs ( 3.09 usr + 0.00 sys = 3.09 CPU) +@ 562.70/s (n= 1741) rot13splitmap: 3 wallclock secs ( 3.29 usr + 0.00 sys = 3.29 CPU) +@ 364.99/s (n= 1199) rot13substrfor: 3 wallclock secs ( 3.22 usr + 0.00 sys = 3.22 CPU) +@ 491.16/s (n= 1584) rot13tr: 4 wallclock secs ( 3.09 usr + 0.00 sys = 3.09 CPU) +@ 61032.31/s (n=188895) Rate rot13splitmap rot13substrfor rot13dynre2 rot13d +ynre rot13builtre rot13tr rot13builttr rot13splitmap 365/s -- -26% -35% +-36% -36% -99% -99% rot13substrfor 491/s 35% -- -13% +-13% -14% -99% -99% rot13dynre2 563/s 54% 15% -- + -1% -2% -99% -99% rot13dynre 568/s 56% 16% 1% + -- -1% -99% -99% rot13builtre 573/s 57% 17% 2% + 1% -- -99% -99% rot13tr 61032/s 16622% 12326% 10746% 10 +649% 10556% -- -1% rot13builttr 61540/s 16760% 12429% 10836% 10 +738% 10645% 1% --
Benchmark:
Update: Reorganization for clarity.
|
|---|