Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Parsing named parameters

by robin (Chaplain)
on Nov 07, 2005 at 15:12 UTC ( #506366=note: print w/ replies, xml ) Need Help??


in reply to Re: Parsing named parameters
in thread Parsing named parameters

As dwildesnl suggested, reversing makes it significantly faster: see benchmark below. But don't do the optimisation unless you need it!

my $data = <DATA>; use Benchmark "cmpthese"; cmpthese(10_000, { "reversed" => sub { my %hash; my $reversed_data = reverse($data); $hash{reverse($2)} = reverse($1) while $reversed_data =~ /\s*([^=]+?)\s*=\s*(\w+)/g; }, "non-reversed" => sub { my %hash; my $data = $data; $hash{$1} = $2 while $data =~ s/\s*(\w+)\s*=\s*([^=]+)$//; }, }); __DATA__ option1 = value0 value1 value2 option3 =value3 value4 option2=value5
gives results:
Rate non-reversed reversed non-reversed 1263/s -- -89% reversed 10989/s 770% --


Comment on Re^2: Parsing named parameters
Select or Download Code
Re^3: Parsing named parameters
by BrowserUk (Pope) on Nov 07, 2005 at 15:18 UTC
    But don't do the optimisation unless you need it!

    What would be the penalty of performing the optimisation if you don't need it?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      What would be the penalty of performing the optimisation if you don't need it?

      Why make something more complicated when it doesn't bring you any benefit? Some obvious disadvantages:

      1. You waste time writing more complicated code, with no benefit for your application;
      2. You make life harder for whoever has to maintain the code later (whether thatís you or someone else), again with no benefit;
      3. The optimised code, being more complicated, is more likely to have bugs;
      4. etc.

        I was enquiring after the specific case of your example code, rather than the general case. How many of those still apply?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        This node was taken out by the NodeReaper on Mon Nov 7 16:36:01 2005 GMT
        Reason: [BrowserUk]: PLease delete--accidental duplicate

        For more information on this node visit: this

        This node was taken out by the NodeReaper on Mon Nov 7 16:49:50 2005 GMT
        Reason: [BrowserUk]: Please delete

        For more information on this node visit: this

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://506366]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-07-10 12:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (209 votes), past polls