Advertisement
DRVTiny

first_bench.pl

Nov 21st, 2017
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.96 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use 5.16.1;
  3. use constant {
  4.     DFLT_ITER_COUNT => 1000,
  5.     DFLT_VEC_LENGTH => 100,
  6. };
  7. use strict;
  8. use Time::HiRes;
  9. use Benchmark qw(:all);
  10. use List::Util qw(first);
  11. use List::Util::XS 1.20;
  12. use Getopt::Std;
  13.  
  14. getopts('i:l:', \my %opt);
  15. my ($iterCount, $vecLength) = ($opt{'i'} || DFLT_ITER_COUNT, $opt{'l'} || DFLT_VEC_LENGTH);
  16.  
  17. say "Preparing sampling matrix ${iterCount}x${vecLength}...";
  18. my @rndNums=map { [map { int(rand($vecLength)) } 1..$vecLength] } 1..$iterCount;
  19. say 'done';
  20.  
  21. say 'Benchmarking...';
  22. timethese($iterCount, {
  23.     'List::Util' => sub {
  24.         my $r=first { ($_&3) == 0  } @{$rndNums[int(rand($iterCount))]}
  25.     },
  26.     'Native::For' => sub {
  27.         my $r;
  28.         !($_&3) and $r=$_, last for @{$rndNums[int(rand($iterCount))]};
  29.         $r
  30.     },
  31.     'Native::Grep' => sub {
  32.         my $r;
  33.         LABEL: { grep { !($_&3) and $r=$_, last(LABEL) } @{$rndNums[int(rand($iterCount))]} }
  34.         $r
  35.     },
  36. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement