// hypothetical struct x
if (flag) {
x->y->z->a->b[5]->q = 8;
x->y->z->a->b[5]->r = 9;
}
####
if (flag) {
t = x->y->z->a->b[5];
t->q = 8;
t->r = 9;
}
####
#!/usr/bin/perl
use strict;
use warnings;
use Time::HiRes;
use Benchmark qw(cmpthese :hireswallclock);
my @bogus;
$bogus[1]{foo}[2]{bar}[3]{baz} = 5;
$bogus[1]{foo}[2]{bar}[3]{quux} = 6;
our @bogus2;
$bogus2[1]{foo}[2]{bar}[3]{baz} = 5;
$bogus2[1]{foo}[2]{bar}[3]{quux} = 6;
cmpthese(1_000_000, {
without_common => sub {
my $prod = $bogus[1]{foo}[2]{bar}[3]{baz} *
$bogus[1]{foo}[2]{bar}[3]{quux};
},
with_common => sub {
my $common = $bogus[1]{foo}[2]{bar}[3];
my $prod = $common->{baz} * $common->{quux};
},
without_common_g => sub {
my $prod = $bogus2[1]{foo}[2]{bar}[3]{baz} *
$bogus2[1]{foo}[2]{bar}[3]{quux};
},
with_common_g => sub {
my $common = $bogus2[1]{foo}[2]{bar}[3];
my $prod = $common->{baz} * $common->{quux};
},
});
####
Rate without_common_g without_common with_common_g with_common
without_common_g 341880/s -- -4% -20% -23%
without_common 357910/s 5% -- -16% -19%
with_common_g 428449/s 25% 20% -- -3%
with_common 443853/s 30% 24% 4% --
####
$"=$,,$_=q>|\p4<6 8p
.q>.<4-KI;$,
.=pack'N*',"@{[unpack'C*',$_]
}"for split/;$_=$,,y[A-Z a-z]
{}cd;print lc