As long as we're (micro)optimizing, why even take references to the elements of @_ at all? Just shuffle indices of @_ and grab things out of @_ as they are needed. I guess indexing an array is a tiny bit faster than following a scalar ref.
sub blokhead (@) {
my @a = (0 .. $#_);
my $i = @_;
my $n;
map+( $n=rand($i--), $_[$a[$n]], $a[$n]=$a[$i] )[ 1 ], @_;
}
It's clearly not a significant improvement, but does seem to be consistently a tiny bit faster...
On large data:
our @data = map { 'x' x 1000 } 1..1000;
cmpthese -2, { map { $_ => "$_ \@data" } qw/naive listutil buk ikegami
+ blokhead/ };
Rate naive ikegami listutil buk blokhead
naive 76.1/s -- -26% -47% -88% -88%
ikegami 103/s 36% -- -28% -84% -84%
listutil 143/s 89% 39% -- -78% -78%
buk 638/s 739% 518% 345% -- -1%
blokhead 643/s 745% 522% 348% 1% --
On small data:
our @data = ("xxx") x 1000;
cmpthese -2, { map { $_ => "$_ \@data" } qw/naive listutil buk ikegami
+ blokhead/ };
Rate naive listutil ikegami buk blokhead
naive 328/s -- -26% -44% -49% -49%
listutil 445/s 36% -- -24% -30% -31%
ikegami 589/s 80% 32% -- -8% -9%
buk 637/s 94% 43% 8% -- -1%
blokhead 646/s 97% 45% 10% 1% --
-
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.