in reply to Re: Show All Characters in Text
in thread Show All Characters in Text
All good points. Here is a little Benchmark that makes your point:
use Benchmark; $iterations = 1000000; $name1 = "Alternation"; $code1 = ' $_="ABBA"; s/(A|B)/X/g; '; $name2 = "Class"; $code2 = ' $_="ABBA"; s/[AB]/X/g; '; $name3 = "Substitution"; $code3 = ' $_="ABBA"; s/A/X/g; '; $name4 = "Transliterate"; $code4 = ' $_="ABBA"; tr/A/X/; '; timethese($iterations, {$name1 => $code1, $name2 => $code2, $name3 => $code3, $name4 => $code4, } ); __END__ C:\>perl test.pl Benchmark: timing 1000000 iterations of Alternation, Class, Substituti +on, Transliterate... Alternation: 36 wallclock secs (35.92 usr + 0.00 sys = 35.92 CPU) @ + 27839.64/s (n=1000000) Class: 23 wallclock secs (23.12 usr + 0.00 sys = 23.12 CPU) @ + 43252.60/s (n=1000000) Substitution: 20 wallclock secs (20.81 usr + 0.00 sys = 20.81 CPU) @ + 48053.82/s (n=1000000) Transliterate: 8 wallclock secs ( 8.07 usr + 0.00 sys = 8.07 CPU) @ + 123915.74/s (n=1000000) C:\>
# mine.pl sub show { my @data = @_; for (@data) { s/\266/%B6/g; s/\267/%B7/g; tr/ /\267/; s/\t/ -> /g; s/\n/\266\n/g; } return wantarray ? @data : join'',@data; } @data = (<DATA>)[0..7]; my $start = time; for (0..100000) { my @new = show(@data) } printf "Mine takes %d seconds", (time - $start); __DATA__ tab 4 spaces, trailing tab tab and 4 spaces -> tab¶ ····4·spaces,·trailing·tab -> ¶ -> ····tab·and·4·spaces¶ -> -> ¶ ¶ # yours.pl my %mapping = ( "\t" => ' -> ', " " => chr(0267), "\n" => chr(0266)."\n", "\266" => '%B6', "\267" => '%B7', ); my $pattern = qr/([@{[join '', keys %mapping]}])/; sub show { my @data = @_; s/$pattern/$mapping{$1}/g for (@data); return wantarray ? @data : join'',@data; } @data = (<DATA>)[0..7]; my $start = time; for (0..100000) { my $new = show(@data) } printf "Yours takes %d seconds", (time - $start); __DATA__ tab 4 spaces, trailing tab tab and 4 spaces -> tab¶ ····4·spaces,·trailing·tab -> ¶ -> ····tab·and·4·spaces¶ -> -> ¶ ¶
You can't use Benchmark fairly on yours as the hash set up is a once only so I just use time and run a loop on the respective subs. If you run this code you will notice that my code takes half as long as yours as there is no hash look up involved. Had to salvage a little face :-)
TIMTOWTDI!
C:\>perl mine.pl Mine takes 59 seconds C:\>perl yours.pl Yours takes 119 seconds C:\>
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|
|---|