in reply to Speeding up a Perl code. Can it be faster?

Thank you all for help and showing me more ways to do the same thing faster.
From tweaking this small code I managed to speed it up about three times faster, just moving some stuff around.

sub forth_draft { my %xlist = map{ split('\/', $_) } split(',', $match_list); $attrs =~ s/\A$tag //; # clean up for (split (' (?=\w+\=)', $attrs)) { my ($name, $value) = split('='); # little more golfing action (defined $xlist{$name} && $markup =~ s/X\{$name\}/$value/g) ? $xlist{$name} = undef # this exists now : $markup = '' if $markup; # do this and let it run through, fas +ter. last if !$markup; # with out this it drops speed now? } # not in Benchmark # $markup # ? print $markup # : print 'nope'; }
Test output:
Benchmark: running a_first_draft, b_second_draft, c_third_draft, d_for +th_draft for at least 1 CPU seconds... a_first_draft: 1 wallclock secs ( 1.05 usr + 0.00 sys = 1.05 CPU) @ + 29132.76/s (n=30502) b_second_draft: 1 wallclock secs ( 1.09 usr + 0.00 sys = 1.09 CPU) +@ 31449.73/s (n=34406) c_third_draft: 1 wallclock secs ( 1.11 usr + 0.00 sys = 1.11 CPU) @ + 35255.18/s (n=39098) d_forth_draft: 1 wallclock secs ( 1.06 usr + 0.00 sys = 1.06 CPU) @ + 115597.37/s (n=122880)

Replies are listed 'Best First'.
Re^2: Speeding up a Perl code. Can it be faster? (-50%)
by BrowserUk (Patriarch) on Jun 11, 2016 at 12:27 UTC

    This is 6x faster than your original and 50% faster than your latest (and a whole lot more readable to boot):

    use strict; use warnings; use Data::Dump qw[ pp ]; use Benchmark qw[ cmpthese ]; my $match_list = 'a/ere,b/ere,c/ere,d/ere,e/ere,f/ere,g/ere,h/ere'; my @markup = ( 'TY:' . 'X{a} X{b} X{c} X{d} X{e} X{f} X{g} X{h} + ' x 100 ) x 3; my @attrs = ( 'TY a=alpha b=bravo c=charlie d=delta e=echo f=fox +trot g=golf h=hotel' ) x 3; my $tag = 'TY'; my $iters = $ARGV[ 0 ] // -1; sub buk { my %xlist = map split( m'/', $_ ), split ',', $match_list; $attrs[ 0 ] =~ s[^$tag ][]; for( split m' ', $attrs[ 0 ] ) { my( $name, $value ) = split '='; ( defined $xlist{ $name } && $markup[ 0 ] =~ s[X\{$name\}][$va +lue]g ) or $markup[ 0 ] = '', last; undef $xlist{ $name }; } $iters == 1 and print "0:$markup[ 0 ]\n" } sub first_draft { my %xlist = map{ split m[/], $_, 2 } grep{ m[/] } split ',', $mat +ch_list; $attrs[ 1 ] =~ s[\A$tag ][]; # clean up my @attr = $attrs[ 1 ] =~ m[(?:\A| )(.+?)(?=(?: \w+=|\z))]g; for( @attr ) { my( $name, $value ) = split '=', $_; # only one attribute of the same name is allowed. if( exists $xlist{ $name } && defined $xlist{ $name } ) { $xlist{ $name } = undef; # this exists now $markup[ 1 ] =~ s[X\{$name\}][$value]g; } else { # fails get nothing and end. $markup[ 1 ] = ''; last; } } $iters == 1 and print "1:$markup[ 1 ]\n" } sub forth_draft { my %xlist = map{ split('\/', $_) } split(',', $match_list); $attrs[ 2 ] =~ s/\A$tag //; # clean up for (split (' (?=\w+\=)', $attrs[ 2 ])) { my ($name, $value) = split('='); # little more golfing action (defined $xlist{$name} && $markup[ 2 ] =~ s/X\{$name\}/$value/g) ? $xlist{$name} = undef # this exists now : $markup[ 2 ] = '' if $markup[ 2 ]; # do this and let it run th +rough, faster. last if !$markup[ 2 ]; # with out this it drops speed now? } $iters == 1 and print "2:$markup[ 2 ]\n" } cmpthese( $iters, { 'first_draft' => \&first_draft, 'forth_draft' => \&forth_draft, buk => \&buk, } ); __END__ C:\test>junk -1 Rate first_draft forth_draft buk first_draft 4640/s -- -79% -86% forth_draft 22226/s 379% -- -33% buk 33339/s 619% 50% --

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice. Not understood.
Re^2: Speeding up a Perl code. Can it be faster?
by oiskuu (Hermit) on Jun 11, 2016 at 15:49 UTC

    Your first_draft() and forth_draft() produce differing results when an attribute is missing from $markup.

    At this point, the exercise appears as one of futility. We don't know what the problem is, which parameters are global, which are variable. We don't know what the inputs are to the function, or even if it's a function that you need in the first place.

    Please concentrate on the correctness before tackling the overhead. Please expound on the actual problem before coding up a solution. Maybe we can comment on that and offer alternatives.