in reply to Re: Trying to optimize de-referenced hash slice without scope variables...
in thread Trying to optimize de-referenced hash slice without scope variables...

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 @dummy = @{$hashref}{'Lore','lors','nsec'}; } sub WithoutCopy { my @dummy = @{$_[0]}{'Lore','lors','nsec'}; } cmpthese(5000000, { 'WithCopy' => 'WithCopy(\%data);', 'WithoutCopy' => 'WithoutCopy(\%data);', }); __END__ Rate WithCopy WithoutCopy WithCopy 548908/s -- -6% WithoutCopy 581801/s 6% --
  • Comment on Re^2: Trying to optimize de-referenced hash slice without scope variables...
  • Download Code

Replies are listed 'Best First'.
Re^3: Trying to optimize de-referenced hash slice without scope variables...
by Joost (Canon) on Jun 15, 2004 at 18:04 UTC
    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.