On my machine that gives (with 500000 iterations)
Rate WithCopy WithoutCopy WithCopy 450450/s -- -9% WithoutCopy 495050/s 10% --
But if you access the hashref more than once in the functions - say about 10 times:
use strict; use Benchmark qw(:all); my %data = ( Lore =>'mipsumdo', ente =>'squemoll', lors =>'itametco', isod =>'ioetnonu', nsec =>'tetuerad', mmyf =>'acilisis', ipis =>'cingelit', augu =>'eliberoi', Sedr =>'honcusma', acul =>'isodioat', ssai =>'dmollisp', cons =>'ectetuer', hare =>'travelit', nisl =>'nislquis', null =>'amattise', feli =>'sDonecma', nimq =>'uisferme', gnar =>'isusulla', ntum =>'nequenul', mcor =>'peridele', lase =>'dduiDone', ifen =>'dutfeugi', cbla =>'nditmetu', atas =>'emAliqua', svit =>'aecondim', msed =>'magnaado', entu =>'mluctusa', lorn =>'onummysa', ntem =>'assaeuis', gitt =>'isNuncne', moda =>'nteaport', corc =>'iMorbima', amii =>'psumnonl', ttis =>'blandits', eoAl =>'iquamcon', emPh =>'asellusq', dime =>'ntumblan', uiso =>'rciInfer', ditf =>'elisPell', ment =>'umturpis', ); sub WithCopy { my $hashref = $_[0]; my @dummy1 = @{$hashref}{'Lore','lors','nsec'}; my @dummy2 = @{$hashref}{'Lore','lors','nsec'}; my @dummy3 = @{$hashref}{'Lore','lors','nsec'}; my @dummy4 = @{$hashref}{'Lore','lors','nsec'}; my @dummy5 = @{$hashref}{'Lore','lors','nsec'}; my @dummy6 = @{$hashref}{'Lore','lors','nsec'}; my @dummy7 = @{$hashref}{'Lore','lors','nsec'}; my @dummy8 = @{$hashref}{'Lore','lors','nsec'}; my @dummy9 = @{$hashref}{'Lore','lors','nsec'}; my @dummy0 = @{$hashref}{'Lore','lors','nsec'}; } sub WithoutCopy { my @dummy1 = @{$_[0]}{'Lore','lors','nsec'}; my @dummy2 = @{$_[0]}{'Lore','lors','nsec'}; my @dummy3 = @{$_[0]}{'Lore','lors','nsec'}; my @dummy4 = @{$_[0]}{'Lore','lors','nsec'}; my @dummy5 = @{$_[0]}{'Lore','lors','nsec'}; my @dummy6 = @{$_[0]}{'Lore','lors','nsec'}; my @dummy7 = @{$_[0]}{'Lore','lors','nsec'}; my @dummy8 = @{$_[0]}{'Lore','lors','nsec'}; my @dummy9 = @{$_[0]}{'Lore','lors','nsec'}; my @dummy0 = @{$_[0]}{'Lore','lors','nsec'}; } cmpthese(500000, { 'WithCopy' => 'WithCopy(\%data);', 'WithoutCopy' => 'WithoutCopy(\%data);', }); __END__ Rate WithoutCopy WithCopy WithoutCopy 82645/s -- -8% WithCopy 89445/s 8% --

Which seems to show that the performance increase is less when you index the @_ array a lot.

Of course it doesn't show that - it could also mean that the time needed to copy the keys of the hash reference 10 times overshadows the time needed to index the array and/or copy the hash reference, thereby decreasing the influence of doing either.

Benchmarking is hard.


In reply to Re^3: Trying to optimize de-referenced hash slice without scope variables... by Joost
in thread Trying to optimize de-referenced hash slice without scope variables... by monsieur_champs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.