I have a small section of code that works. But I would like it to be faster. The size of the data will stay around its current size to maybe four times that. If that is any help in optimizing the code. Here is the working code in a Benchmark test.
Thank you in advance.use strict; use warnings; use Data::Dumper; use Benchmark; # verification pattern is used by the developer to set a range # in this case the first letter before the slash is to verify # the attribute name in $attrs. The content after the slash # is used to verify the attribute was not used yet. but in the full # version ere is a range that goes to a function to verify the # attribute values. my $match_list = 'a/ere,c/ere,d/ere'; # in $attrs the letter before equal is the attribute name # and needs to be compared with $match_list for its value to be replac +ed. # all attribute names can only be used one time in $attrs to be good # the value of the attribute may have a space in it. my $attrs = 'TY a=errt c=rrr dd d=rrr dd'; # example that should conve +rt #my $attrs = 'TY a=errt c=rrr dd d=rrr d=du 0'; # example that should +say "nope" # tag name, just something that is there. my $tag = 'TY'; # markup is the template of the output if its good my $markup = 'TY:X{a} X{c}|X{d}'; # few more examples based off some of your posts sub third_draft { my %xlist = map{ split('\/', $_) } split(',', $match_list); $attrs =~ s/\A$tag //; # clean up for (split (' (?=\w+\=)', $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'; } sub second_draft { my %xlist = map{ split(/\//, $_) } # make list split(/,/, $match_list); $attrs =~ s/\A$tag //; # clean up my @attr = $attrs =~ /(?:\A| )(.+?)(?=(?: \w+=|\z))/g; # make arra +y for xlist 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 =~ s/X\{$name\}/$value/g; } else { # fails get nothing and end. $markup = ''; last; } } # not in Benchmark # $markup # ? print $markup # : print 'nope'; } sub first_draft { my %xlist = map{ split(/\//, $_, 2) } # make list grep{ m/\// } split(/,/, $match_list); $attrs =~ s/\A$tag //; # clean up my @attr = $attrs =~ /(?:\A| )(.+?)(?=(?: \w+=|\z))/g; # make arra +y for xlist 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 =~ 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 ? ( 'a_first_draft' => \&first_draft, ) : (), 1 ? ( 'b_second_draft' => \&second_draft, ) : (), 1 ? ( 'c_third_draft' => \&third_draft, ) : (), });
Update: Added better comments in code.
In reply to Speeding up a Perl code. Can it be faster? by $h4X4_|=73}{
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |