in reply to Is this a fair shuffle?
It is fair*, and pretty slick for a pure perl implementation too, but the XS version of List::Util shuffle is 6x faster.
Even if you do an in-place version,
sub sm{ my $n = @{ $_[ 0 ] }; push @{ $_[ 0 ] }, splice @{ $_[ 0 ] }, rand $n--, 1 while $n; }
you gain very little.
* That is to say, it is a correct implementation of the Fischer-Yates shuffle, and is therefore fair.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is this a fair shuffle?
by Abigail-II (Bishop) on May 02, 2004 at 23:20 UTC | |
I've been pushing the Fisher-Yates shuffle instead of the splicing shuffle since 1995 or so. Since then, it has made its way into the FAQ, we have List::Util::shuffle, but despite the FAQ spelling out what's wrong with the splicing algorithm, that one just doesn't want to die. Abigail | [reply] |
by BrowserUk (Patriarch) on May 03, 2004 at 06:08 UTC | |
Okay Abigail, I agree with you on the O(nē) thing with regard to the performance of the implementation of the splice versions, though it didn't seem "slow" in my original tests. I ran tests with 10, 100, & 1000 elements, and as well as beating the pure Perl implementations of F_Y comfortably, nothing in the numbers actually screamed "quadratic" at me.
However, since reading your post, I did runs of 10_000 and 100_000 and only now the difference begins to show up.
And that shows the transition quite dramatically. In my defense, I was only really checking for it's fairness which I did using the code you'll recognise from an old post of yours.
The only performance issue I considered was relative to the List::Util XS implementation. It was, as expected, considerably slower and that was the main point. Now the bit where I got confused. I thought about the difference between, say the pure-perl/copying and the Splicing/copying versions, and the main difference is that the former swaps contents of elements whereas that latter swaps linked elements. I concluded that the difference between the two was an "implementation detail", in the same way as the difference between the pure-perl/copying and the XS version is an implementation detail, and therefore didn't change the nature of the basic algorithm being used, hence the addendum of "it's a Fischer Yates". I was wrong! I'm still a little bemused by why swapping pointers on the linked list, rather than swapping the contents of the elements the linked list points at, becomes quadratic, but the (newer) numbers demonstrate your point. I will have to sit down with a pen and paper and the source code of splice to understand why the costs grow that way. So, thanks for setting me straight. Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham"Think for yourself!" - Abigail | [reply] [d/l] [select] |
by Abigail-II (Bishop) on May 03, 2004 at 09:36 UTC | |
Abigail | [reply] |