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:\>

But if we are going to get into a little recreational optimisation.....

# 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


In reply to Re: Re: Show All Characters in Text by tachyon
in thread Show All Characters in Text by tachyon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.