develooper Front page | perl.perl5.porters | Postings from April 2010

[perl #74650] perlperf.pod wrong example

From:
Daniel Pfeiffer
Date:
April 26, 2010 03:08
Subject:
[perl #74650] perlperf.pod wrong example
Message ID:
rt-3.6.HEAD-6227-1272201664-860.74650-75-0@perl.org
# New Ticket Created by  Daniel Pfeiffer 
# Please include the string:  [perl #74650]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=74650 >


In the dereference benchmark you make exactly the mistake you warn of 
before: you change several factors at once and show an overdrawn 
conclusion.  While refactoring out the common $ref->{ref} to access it 
only once, you unnecessarily also copy both strings to a local 
variable.  It is this second action that drives the time up, if you 
leave that out and run timethese several times your findings are still 
right, but far less.  If however you need a third or more values (which 
I added of the same length), dereferencing the outer hash only once 
becomes a clear winner:

    timethese(1000000, {
        '2direct'      => sub {
            my $x = $ref->{ref}{_myscore} . $ref->{ref}{_yourscore} ;
        },
        '2wrong'       => sub {
            my $ref = $ref->{ref};
            my $myscore = $ref->{_myscore};        # why?
            my $yourscore = $ref->{_yourscore};    # why?
            my $x = $myscore . $yourscore;
        },
        '2dereference' => sub {
            my $ref = $ref->{ref};
            my $x = $ref->{_myscore} . $ref->{_yourscore};
        },
        '3direct'      => sub {
            my $x = $ref->{ref}{_myscore} . $ref->{ref}{_yourscore} .
                $ref->{ref}{_ourscore};
        },
        '3dereference' => sub {
            my $ref = $ref->{ref};
            my $x = $ref->{_myscore} . $ref->{_yourscore} .
                $ref->{_ourscore};
        },
    });

Btw. I have left out the intermediate arrow, making it less of an 
eyesore and easier to understand as a two dimensional hash.

coralament / best Grötens / liebe Grüße / best regards / elkorajn salutojn
Daniel Pfeiffer

-- 
lerne / learn / apprends / lär dig / ucz się    Esperanto:
                    http://lernu.net  /  http://ikurso.net




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About