Skip to content

Instantly share code, notes, and snippets.

@hiratara
Created December 7, 2017 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiratara/0712b007b5ac3a1f67d28765df529ac5 to your computer and use it in GitHub Desktop.
Save hiratara/0712b007b5ac3a1f67d28765df529ac5 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use Benchmark qw(cmpthese);
use Iterator::Simple qw(iter islice imap list);
my $n = 1000;
my $size = 10000;
my $start = $ARGV[0] // die;
my $end = $ARGV[1] // die;
my @list = (1 .. $size);
cmpthese $n => {
array_ref => sub {
my $x = \@list;
my $y = [map { $_ * 2 } @$x];
[@$y[$start .. $end]];
},
array => sub {
my @y = map { $_ * 2 } @list;
[@y[$start .. $end]];
},
iter => sub {
my $x = iter(\@list);
my $y = imap { $_ * 2 } $x;
my $res = $y->slice($start, $end + 1, 1);
[@{list($res)}];
},
};
__END__
$ perl benchmark.pl 0 9999
Rate iter array array_ref
iter 97.0/s -- -59% -62%
array 236/s 144% -- -8%
array_ref 257/s 165% 9% --
$ perl benchmark.pl 0 999
Rate array array_ref iter
array 621/s -- -11% -37%
array_ref 699/s 13% -- -29%
iter 980/s 58% 40% --
$ perl benchmark.pl 9999 9999
Rate iter array array_ref
iter 193/s -- -77% -78%
array 847/s 340% -- -5%
array_ref 893/s 363% 5% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment