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

[perl #74650] perlperf.pod wrong example

Daniel Pfeiffer
April 26, 2010 03:08
[perl #74650] perlperf.pod wrong example
Message ID:
# New Ticket Created by  Daniel Pfeiffer 
# Please include the string:  [perl #74650]
# in the subject line of all future correspondence about this issue. 
# <URL: >

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} .
        '3dereference' => sub {
            my $ref = $ref->{ref};
            my $x = $ref->{_myscore} . $ref->{_yourscore} .

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:
            / Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About