Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Re: Re: Performance of Perl references

by Felonious (Chaplain)
on Jul 22, 2002 at 16:32 UTC ( #184116=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Performance of Perl references
in thread Performance of Perl references

Any time you derefernce to get to a value pointed to by a reference, it's called a layer of indirection. Every layer of indirection adds some overhead, so if you are acccessing data deep inside a complex data structure, it adds up. Normally this is negligable, but if you are doing so inside loops you can gain more speed by removing as many layers of indirecion as possible.
Take the following code sample. The structure '$data' is two levels deep, and the subs 'foo' and 'bar' both loop thru and access all the data at the deepest level. 'foo' uses a naive aproach of multiple layers of indirection at the deepest level 'my $val = $data->{$key1}->{$key2}', while 'bar' reduces the indirection by assigning the inner data struct to a variable 'my $data2 = $data->{$key1}' and looping over it, accessing that data with 'my $val = $data2->{$key2}'.


Benchmark: timing 100000 iterations of multiple_indirection, reduced_indirection...
multiple_indirection: 29 wallclock secs (28.08 usr +  0.00 sys = 28.08 CPU) @ 3561.25/s (n=100000)
reduced_indirection: 25 wallclock secs (24.19 usr +  0.00 sys = 24.19 CPU) @ 4133.94/s (n=100000)
                       Rate multiple_indirection  reduced_indirection
multiple_indirection 3561/s                   --                 -14%
reduced_indirection  4134/s                  16%                   --

#!/usr/bin/perl my %innerdata; @innerdata{a..z} = a..z; my $data = { foo => { %innerdata }, bar => { %innerdata } }; sub foo { my $data = $_[0]; for $key1 (keys %$data) { for $key2 (keys %{$data->{$key1}}) { my $val = $data->{$key1}->{$key2}; # process $val; # print $val; } } } sub bar { my $data = $_[0]; for $key1 (keys %$data) { my $data2 = $data->{$key1}; for $key2 (keys %$data2) { my $val = $data2->{$key2}; # process $val; # print $val; } } } use Benchmark qw(cmpthese); cmpthese( 100000, { multiple_indirection => sub { foo($data) }, reduced_indirection => sub { bar($data) }, } )
-- O thievish Night, Why should'st thou, but for some felonious end, In thy dark lantern thus close up the stars? --Milton

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://184116]
[robby_dobby]: marto: Good morning, I usually spend my Fridays here cursing the bloody weather!
[marto]: where are you based robby_dobby?
[robby_dobby]: marto: Oh, I see you're from an even colder north :-)
[robby_dobby]: marto: Ldn, right now
[marto]: icy here today, snow on the hills
[Corion]: Oh yes, Friday!
[Corion]: Last day at $work before 3 weeks of vacation (well, not that I'd be really travelling, but no-work-time ;) )

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2017-12-15 10:31 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (430 votes). Check out past polls.