This runs about 20% faster for me, YMMV:

use strict; use warnings; use Benchmark; my $match_list = 'a/ere,c/ere'; # verification pattern my $markup = 'TY:X{a} X{c}'; # output markup my $attrs = 'TY a=errt c=rrrdd'; # info to check my $tag = 'TY'; # tag name sub first_draft { my %xlist = map{ split('/', $_, 2) } # make list grep{ m'/' } split(',', $match_list); $attrs =~ s/\A$tag //; # clean up for (split (' ', $attrs)) { my ($name, $value) = split('='); # only one attribute of the same name is allowed. if (defined $xlist{$name}) { $xlist{$name} = undef; # this exists now $markup =~ s/X{$name}/$value/g; } else { # fails get nothing and end. $markup = ''; last; } } # not in Benchmark # $markup # ? print $markup # : print 'nope'; } timethese($ARGV[0] || -1, { 1 ? ( 'first_draft' => \&first_draft, ) : (), });

I would expect there to be a more efficient way to set up %xlist but it hasn't occurred to me yet precisely how.


In reply to Re: Speeding up a Perl code. Can it be faster? by hippo
in thread Speeding up a Perl code. Can it be faster? by $h4X4_|=73}{

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.