Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Speed/Efficiency tweaks for a fannkuch benchmark script?

by robin (Chaplain)
on Dec 02, 2005 at 12:37 UTC ( #513577=note: print w/ replies, xml ) Need Help??


in reply to Speed/Efficiency tweaks for a fannkuch benchmark script?

I can squeeze another 20% or so out of it actually 23% on my systems with some more micro-optimizations. The most significant change is in the inner loop, where Ive changed

substr( $q, 0, $k ) = reverse substr( $q, 0, $k );
to
$q = reverse(substr($q, 0, $k)) . substr($q, $k);
Here's the code:
sub fannkuch { use bytes; # This makes it fractionally faster my ( $copy, $level, $split ) = ( @_, 0, 1 ); my ( $index, $next, $length ) = ( $level, $level + 1, length( $cop +y ) ); if ($next == $length) { $index = $split - 1; substr($copy, $index, 0) = chop($copy); } my ( $q, $k ); do { if ($next == $length) { if (($k = ord($q = $copy)) != $length || $level >= $maxflips) { # Declaring $flips in here means we can reset it # with a single op (compared with the three you # need for C<$flips = 0>). my $flips; # This is a touch faster than a "proper" loop, # because it doesn't push a new context. $q = reverse(substr( $q, 0, $k )) . substr($q, $k), ++$flips while ($k=ord($q)) != 1; no warnings "uninitialized"; # $flips may be undef if ( $flips >= $maxflips ) { if ( $flips == $maxflips) { push @max_sequence, $copy; } else { ($maxflips, @max_sequence) = ($flips, $copy); } } } } else { fannkuch( $copy, $next, $split ); $split = $next if $index == $split; } substr($copy, $index-1, 2) = reverse substr($copy, $index-1, 2 +); } while $index--; $maxflips; # faster than an explicit return }


Comment on Re: Speed/Efficiency tweaks for a fannkuch benchmark script?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://513577]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2014-09-30 14:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (373 votes), past polls