Taking your clever precheck a stage further gives a 50%-ish speed improvement on my iBook:
(
Update: On a faster Linux PC the improvement is much less pronounced: only about 8–10%.)
use List::Util qw( min max );
sub fannkuch {
my ( $aref, $level ) = ( @_, 0 );
my ( $index, $copy, $ok ) = ( $level, [@$aref], $level + 1 == @$ar
+ef );
do {
if ($ok) {
if (max(@$copy[0..($copy->[0] - 1)]) != $copy->[0]
&& min(@$copy[($copy->[-1] - 1)..$#$copy]) != $copy->[-1
+]) {
my @q = @$copy;
my ( $k, $flips );
for ( $flips = 0 ; ( $k = $q[0] ) != 1 ; $flips++ ) {
@q[ 0 .. $k-1 ] = reverse @q[ 0 .. $k-1 ];
}
if ( $flips > $maxflips ) {
$maxflips = $flips;
@max_sequence = ();
}
push @max_sequence, join '', @$copy, "\n"
if ( $maxflips == $flips );
}
}
else {
fannkuch( $copy, 1 + $level );
}
@$copy[ $index - 1, $index ] = @$copy[ $index, $index - 1 ]
if $index != 0;
} while $index-- > 0;
return $maxflips;
}
Incidentally, I tried changing the code to use Algorithm::FastPermute (which implements the same permutation algorithm in C) and the runtime actually increased. I don't know why that's happening.
It may be an unfortunate side-effect of the stability improvements in my latest version of A::FP, or it may be something else entirely. Update: no, it's nothing to do with the recent changes. I get the same result using an old version too.
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, details, 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, summary, 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.