Skip to content

Instantly share code, notes, and snippets.

@bessarabov
Last active December 23, 2020 19:15
Show Gist options
  • Save bessarabov/2eabb33b162cc11bad97c2ccf9e6f2e4 to your computer and use it in GitHub Desktop.
Save bessarabov/2eabb33b162cc11bad97c2ccf9e6f2e4 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw(cmpthese);
cmpthese(
-5,
{
array => sub {
my @arr;
foreach my $i (1..500) {
$arr[$i] = $i;
}
},
hash => sub {
my %h;
foreach my $i (1..500) {
$h{$i} = $i;
}
},
},
);
__END__
$ perl perl_benchmark_array_hash_write.pl
Rate hash array
hash 3613/s -- -87%
array 27587/s 664% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment