Skip to content

Instantly share code, notes, and snippets.

@sugar84
Created October 9, 2012 12:03
Show Gist options
  • Save sugar84/3858366 to your computer and use it in GitHub Desktop.
Save sugar84/3858366 to your computer and use it in GitHub Desktop.
plain vs reference
#!/usr/bin/env perl
use Modern::Perl;
use Benchmark qw/cmpthese/;
my (@arr1, $arr_ref1, %hash1, $hash_ref1);
say "Read access: ";
my $x;
cmpthese(-2, {
hash => sub {
for my $k (keys %hash1) {
$x = $hash1{$k};
}
},
hash_ref => sub {
for my $k (keys %hash1) {
$x = $hash_ref1->{$k};
}
},
array => sub {
for my $i (0 .. $#arr1) {
$x = $arr1[$i];
}
},
array_ref => sub {
for my $i (0 .. $#arr1) {
$x = $arr_ref1->[$i];
}
},
});
say "\n";
say "Write access: ";
cmpthese(-2, {
hash => sub {
for my $i (0 .. 100) {
$hash1{$i} = $i;
}
},
hash_ref => sub {
for my $i (1 .. 100) {
$hash_ref1->{$i} = $i;
}
},
array => sub {
for my $i (1 .. 100) {
$arr1[$i] = $i;
}
},
array_ref => sub {
for my $i (1 .. 100) {
$arr_ref1->[$i] = $i;
}
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment