Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^2: About List::Util's pure Perl shuffle()

by blazar (Canon)
on Jul 11, 2007 at 16:15 UTC ( #626035=note: print w/replies, xml ) Need Help??

in reply to Re: About List::Util's pure Perl shuffle()
in thread 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.

I'm not saying it isn't. (Well, except that some comments for the benefit of the occasional peeping Tom would have been nice.) In fact I'm amazed, because I wouldn't have expected it to be that efficient. The question still is: how does one come up with such code? I suspect that one critical difference is that the naive implementation, due to its de facto shuffling of an array in place, does so by taking two slices, and assigning to one of them. While L::U's doesn't really swap, and the overhead of dereferencing weights less. BTW, testing with

for my $i (map 10**$_, 1..4) { cmpthese -30, { map { $_ => "$_ 1..$i" } qw/naive buk/ }; }

one seems to get a slowly decreasing trend:

Rate naive buk naive 49811/s -- -37% buk 79547/s 60% -- Rate naive buk naive 4751/s -- -33% buk 7112/s 50% -- Rate naive buk naive 479/s -- -29% buk 677/s 41% -- Rate naive buk naive 46.9/s -- -27% buk 64.6/s 38% --

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


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://626035]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2017-05-27 09:10 GMT
Find Nodes?
    Voting Booth?