Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use 5.16.1;
- use constant {
- DFLT_ITER_COUNT => 1000,
- DFLT_VEC_LENGTH => 100,
- };
- use strict;
- use Time::HiRes;
- use Benchmark qw(:all);
- use List::Util qw(first);
- use List::Util::XS 1.20;
- use Getopt::Std;
- getopts('i:l:', \my %opt);
- my ($iterCount, $vecLength) = ($opt{'i'} || DFLT_ITER_COUNT, $opt{'l'} || DFLT_VEC_LENGTH);
- say "Preparing sampling matrix ${iterCount}x${vecLength}...";
- my @rndNums=map { [map { int(rand($vecLength)) } 1..$vecLength] } 1..$iterCount;
- say 'done';
- say 'Benchmarking...';
- timethese($iterCount, {
- 'List::Util' => sub {
- my $r=first { ($_&3) == 0 } @{$rndNums[int(rand($iterCount))]}
- },
- 'Native::For' => sub {
- my $r;
- !($_&3) and $r=$_, last for @{$rndNums[int(rand($iterCount))]};
- $r
- },
- 'Native::Grep' => sub {
- my $r;
- LABEL: { grep { !($_&3) and $r=$_, last(LABEL) } @{$rndNums[int(rand($iterCount))]} }
- $r
- },
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement