http://qs1969.pair.com?node_id=506366


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.