Created
June 20, 2014 10:16
-
-
Save marramgg/c056abe24c8d1b3b41e9 to your computer and use it in GitHub Desktop.
Benchmark overhead of copying hash reference to a new variable, four times...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl | |
use warnings; | |
use strict; | |
use Benchmark qw( cmpthese ); | |
my $h0 = { | |
'h1' => { | |
'h2' => { | |
'h3' => { | |
'h4' => { | |
'key' => 0, | |
}, | |
}, | |
}, | |
}, | |
}; | |
cmpthese(100_000_000, { | |
'use_direct' => sub { sub_direct() }, | |
'use_ref' => sub { sub_ref() }, | |
}); | |
sub sub_direct { | |
$h0->{'h1'}{'h2'}{'h3'}{'h4'}{'key'} = 1; | |
} | |
sub sub_ref { | |
my $h1 = $h0->{'h1'}; | |
my $h2 = $h1->{'h2'}; | |
my $h3 = $h2->{'h3'}; | |
my $h4 = $h3->{'h4'}; | |
$h4->{'key'} = 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Makes sense... Copy ref to the hash into new variable along with the memory allocation for that new variable.
Should make a variant where I have multiple sub_ref like functions that copy less and less references to check if that is linear.