|P is for Practical|
blazar++. Just checking if anyone is still paying attention :)
Since bukNew() doesn't need to copy the input list--because it shuffles, and therefore mutates, a list of indices generated internally, rather than mutating a copy of the input--it seemes silly to pass arrays in by value forcing their duplication. Most uses of shuffle() are applied to pre-existing arrays. That's why we make a copy of them, or create a list of aliases to them in most versions--simply to avoid the shuffle from modifying the external arrays.
Well, but then that is somewhat obvious, and even the naive implementation would would be better apt at shuffling an array in place, and would thus better be rewritten like this:
(Not that this would make it really faster - I checked and it's still considerably slower than the alternatives presented here.)
But then I think that in any case you would be comparing apples and bananas: for it was somewhat clear that the interface was in terms of "accept a list, return a (shuffled) list"... unless the interface itself was also part of the choice to take. OTOH your tests themselves show that blokhead's solution performs best on large datasets, which is exactly were speed is more likely to matter (well, not strictly, since one may have 10^6 shuffles of 10 elements long lists to do...) - and it has a more intuitive interface, so I would go for it. More precisely I have "decided" that my favourite version is the following modification of blokhead() that can be considered a "hybrid" with another version of yours:
I must say that I've included it in the benchmarks too and while it doesn't perform well compared to blokhead(), buk() and bukNew() in the short list tests, it is comparable with the best on the 1000 strings one, and in two of them it even performed best of all, although I'd rather consider this to be a fluctuation. Why this is so, anyway, is beyond my comprehension.
Peter J. Holzer's reply
The main performance improvement comes from avoiding hash slices. Just replacing @l[$_,$r]=@l[$r,$_]; in your code with a conventional swap via a temporary variable:
gives me a 10 % performance boost.
The rest of the improvement comes from not swapping in-place at all, but building a new array from scratch:
is just as fast as the map (and it does the same thing, really).
Fascinating. I would have expected both changes to make the performance worse, not better. Just shows again that intuition is an unreliable guide on perl performance.