If you're using 5.8.*, then stick with what you have:

c:\test>\perl\bin\perl5.8.6.exe -s junk9.pl -N=1e2 Rate ysth buk tye OP ysth 4371/s -- -47% -49% -75% buk 8269/s 89% -- -3% -53% tye 8542/s 95% 3% -- -52% OP 17779/s 307% 115% 108% -- c:\test>\perl\bin\perl5.8.6.exe -s junk9.pl -N=1e5 Rate ysth buk tye OP ysth 3.75/s -- -36% -39% -64% buk 5.88/s 57% -- -4% -44% tye 6.15/s 64% 5% -- -41% OP 10.5/s 179% 78% 70% --

If you're using 5.10, as unintuative as it sounds, a split/join solution seems to be the fastest. Especially when the strings get up into the megabyte range:

c:\test>\Perl510\bin\perl5.10.0.exe -s junk9.pl -N=1e2 Rate ysth tye OP buk ysth 2326/s -- -50% -64% -72% tye 4670/s 101% -- -27% -45% OP 6417/s 176% 37% -- -24% buk 8419/s 262% 80% 31% -- c:\test>\Perl510\bin\perl5.10.0.exe -s junk9.pl -N=1e5 Rate ysth OP tye buk ysth 1.83/s -- -32% -46% -69% OP 2.70/s 48% -- -21% -55% tye 3.42/s 87% 26% -- -43% buk 5.98/s 227% 121% 75% -- c:\test>\Perl510\bin\perl5.10.0.exe -s junk9.pl -N=1e6 s/iter OP ysth tye buk OP 43.0 -- -54% -60% -96% ysth 19.9 116% -- -14% -91% tye 17.1 151% 16% -- -90% buk 1.72 2400% 1056% 896% --

I'm quite prepared to have my intuition confirmed and benchmark proven wrong, but this is the second time this year I've benchmarked this and I can't see anything wrong with my benchmark.

The thing to note is that the performance of the split/join solution (buk) hardly varies between the two builds, but the cost of the substitutions seems to have really increased in 5.10. With or without capturing parens or /e.

The benchmark code:

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; my %subst = ( '&' => 'amp', '>' => 'gt', '<' => 'lt' ); our $ysth = join('|', map quotemeta, sort { length($b) <=> length($a) +|| $b cmp $a } keys %subst); our $N ||= 1e3; our $data = join( '', map{ 'junk ' . (qw[ & < > ])[ rand 3 ] } 1 .. $ +N ); cmpthese -3, { OP => sub { $_ = $data; s{\&}{amp}g; s{\<}{lt}g; s{\>}{gt}g }, tye => sub { $_ = $data; s{([&<>])}{$subst{$1}}ge; }, ysth => sub { $_ = $data; s{($ysth)}{$subst{$1}}ge; }, buk => sub { $_ = $data . ' '; $_ = join 'amp', split '&'; $_ = join 'lt', split '<'; $_ = join 'gt', split '>'; chop; }, };

Have at it!


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: using tr/// by BrowserUk
in thread using tr/// by chuckd

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.