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:
Update: Reorganization for clarity.
|
---|