Skip to content

Instantly share code, notes, and snippets.

@bessarabov
Created December 23, 2020 19:16
Show Gist options
  • Save bessarabov/ac4fe3562536118f2ec3b75f808be26f to your computer and use it in GitHub Desktop.
Save bessarabov/ac4fe3562536118f2ec3b75f808be26f to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw(cmpthese);
my @arr;
foreach my $i (1..500) {
$arr[$i] = $i;
}
my %h;
foreach my $i (1..500) {
$h{$i} = $i;
}
my $tmp;
cmpthese(
-5,
{
array => sub {
foreach my $i (1..500) {
$tmp = $arr[$i];
}
},
hash => sub {
foreach my $i (1..500) {
$tmp = $h{$i};
}
},
},
);
__END__
$ perl perl_benchmark_array_hash_read.pl
Rate hash array
hash 3720/s -- -88%
array 31209/s 739% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment