Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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 I’ve 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 }

In reply to Re: Speed/Efficiency tweaks for a fannkuch benchmark script? by robin
in thread Speed/Efficiency tweaks for a fannkuch benchmark script? by thundergnat

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-25 19:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found