Algorithm-Permute

 view release on metacpan or  search on metacpan

bench/benchmark.pl  view on Meta::CPAN

#!/usr/bin/perl -w

# for your own pleasure and curiosity
use strict;
use blib;
use Algorithm::Permute 'permute';
use Benchmark ':all';
use Getopt::Std;

# process options
my %opts;
getopts('yrhl:n:', \%opts) or usage();
$opts{h} and usage();
$opts{n} ||= 9;
$opts{l} ||= 5;
my @arr = (1..$opts{n});

# runners
my %runners = (
   'Combinatorial::Permutations' => sub { 
        my @res = Combinatorial::Permutations::permutate(@arr);
    },
    'Memoization' => sub {
        my $num_permutations = PMemoization::factorial(scalar @arr);
        for (my $i=0; $i < $num_permutations; $i++) {
            my @permutation = @arr[PMemoization::n2perm($i, $#arr)];
            # print "@permutation\n";
        }
    },
    'LISPy' => sub { LISPy::faq_permute([@arr], []) },
    'List::Permutor' => sub {
        my $l = new List::Permutor(@arr);
        while (my @res = $l->next) {}
    },
    'Algorithm::Permute' => sub {
        my $p = new Algorithm::Permute([@arr]);
        while (my @res = $p->next) {}
    },
    'Algorithm::Permute qw(permute)' => sub { 
        permute { my @res = @arr } @arr;
    },
    'Algorithm::Combinatorics' => sub { 
        my $i = Algorithm::Combinatorics::permutations(\@arr);
        while (my $p = $i->next) {} 
    },
    'Math::Combinatorics' => sub {
        my $combinat = Math::Combinatorics->new(count => $opts{n}, data => \@arr);
        while (my @res = $combinat->next_permutation) {}
    },
);

my @modules;
# load optional modules
my @optionals = 
    qw/Algorithm::Combinatorics Math::Combinatorics List::Permutor/;
foreach my $m (@optionals) {
    eval "require $m";
    if ($@) {
        print "Unable to load $m. Not yet installed?\n";
    } else {
        print "Module $m loaded.\n";
        push @modules, $m;
    }
}

# give user a chance to select modules to his/her interest
print "\nRun benchmark against:\n";
my @selected = grep {
    print "$_ [Y/n]? "; 
    if ($opts{'y'}) { print "Y\n"; 1 }



( run in 0.310 second using v1.01-cache-2.11-cpan-e1769b4cff6 )