Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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% --

Replies are listed 'Best First'.
Re^3: Parsing named parameters
by BrowserUk (Patriarch) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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 romping around the Monastery: (6)
As of 2023-03-28 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (69 votes). Check out past polls.

    Notices?