http://www.perlmonks.org?node_id=626004


in reply to About List::Util's pure Perl shuffle()

If you accept the principle that code in modules is destined to be reused in lots of situations, including performance critical ones, and should therefore do it's utmost to be as efficient as the author is comfortable maintaining, then List::Util's use of aliasing to sqeeze a little more performance seems entirely reasonable.

That said, they missed a trick. Using a block form map creates a additional level of scope that they aren't using and slows performance.

They can squeeze another 30%, and almost 50% over the 'naive' implementation, by avoiding it:

sub buk (@) { my @a = \( @_ ); my $n; my $i = @_; map+( $n=rand($i--), ${ $a[ $n ] }, $a[ $n ]=$a[ $i ] )[ 1 ], @_; } cmpthese -1, { map { $_ => "$_ 1..1000" } qw/naive listutil buk/ }; __END__ C:\test>junk Rate naive listutil buk naive 520/s -- -13% -32% listutil 597/s 15% -- -22% buk 769/s 48% 29% --

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.