Do you know where your variables are? PerlMonks

### Comment on

 Need Help??
I feel a bit indebted to write the results of a benchmark I made to find out which of the permutation generators are fast, Algorithm::Permute , tye's implementation , dragonchild's implementation and of course my Steinhaus Johnson Trotter XS implementation:
```
Rate   tye   SJT dchld  A::P
tye   1.27/s    --  -22%  -46%  -81%
SJT   1.63/s   28%    --  -30%  -76%
dchld 2.34/s   84%   44%    --  -65%
A::P  6.67/s  423%  310%  185%    --

[download]```
Here's the code I used to make the benchmark:
```use strict;
use warnings;
use Benchmark qw/cmpthese timethese/;
use Algorithm::Permute;
use SJT;

my \$n = 8; # objects to permute
my \$iter=4;

sub nextPermute(\@)
{
my( \$vals )= @_;
my \$last= \$#{\$vals};
return ""   if  \$last < 1;
# Find last item not in reverse-sorted order:
my \$i= \$last-1;
\$i--   until  \$i < 0  ||  \$vals->[\$i] lt \$vals->[\$i+1];
# If complete reverse sort, we are done!
return ""   if  -1 == \$i;
# Re-sort the reversely-sorted tail of the list:
@{\$vals}[\$i+1..\$last]= reverse @{\$vals}[\$i+1..\$last]
if  \$vals->[\$i+1] gt \$vals->[\$last];
# Find next item that will make us "greater":
my \$j= \$i+1;
\$j++  until  \$vals->[\$i] lt \$vals->[\$j];
# Swap:
@{\$vals}[\$i,\$j]= @{\$vals}[\$j,\$i];
return 1;
}

sub make_orderings
{
my \$num = shift;

my @arr = (1 .. \$num);

return sub {
my \$last = \$#arr;

my \$i = \$last - 1;
\$i-- while 0 <= \$i && \$arr[\$i] >= \$arr[\$i+1];
return if \$i == -1;

@arr[\$i+1..\$last] = reverse @arr[\$i+1..\$last]
if \$arr[\$i+1] > \$arr[\$last];

my \$j=\$i+1;
\$j++ while \$arr[\$i] >= \$arr[\$j];

@arr[\$i,\$j] = @arr[\$j,\$i];

return @arr;
}
}

cmpthese(
\$iter,
{
'A::P' => sub {
use Algorithm::Permute;
my \$p = new Algorithm::Permute([1..\$n], \$n);
while (my @res = \$p->next) {
#print join(", ", @res), "\n";
}
},
'SJT'                => sub {
my \$s = SJT->new(\$n);
while(\$s->next_perm()){
my @p = @{\$s->{permutation}};
#\$s->print_perm;
};
},
'tye' => sub {

my @w= (1..\$n);
do {
} while( nextPermute(@w) );
},
'dchld' => sub {
my \$i = make_orderings(\$n);
while(my @a = \$i->()){
};
},
}
);
[download]```

In reply to Re: Permuting with duplicates and no memory by spx2
in thread Permuting with duplicates and no memory by tye

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Please read these before you post! —
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.
• Log In?
 Username: Password:

What's my password?
Create A New User
Chatterbox?
 [chacham]: I'm sure this is old, but i just came across it: If you put a million monkeys at a million keyboards, one of them will eventually write a Java program. The rest of them will write Perl programs. [LanX]: lol [Corion]: Woo hoo! A new Auralnauts episode! LanX mean unfair and funny

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2018-04-23 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (85 votes). Check out past polls.

Notices?