And you really think numbers like "19019%" mean you have a brilliant algorithm? Just accept that, don't ask any questions?

Think again. Next time, try to run your sort in a list context instead of a void context (when Perl will just optimize the entire sort away) and see what happens:

#!/usr/bin/perl -w use strict; use Benchmark qw(cmpthese); use Data::Dumper; use List::Util qw /shuffle/; sub xform { map {$_->[0]} sort { $a->[1] <=> $b->[1]} map {[$_, rand(1)]} @_; } sub slice { my @random; push @random, rand 1 for 0 .. $#_; @_[ sort { $random[$a] <=> $random[$b] } 0 .. $#_ ]; } sub shufl { $a = $_ + rand @_ - $_ and @_[$_, $a] = @_[$a, $_] for (0..$#_); return @_; } sub qshuf { my %hash; sort { ($hash{$a}||=rand 1) <=> ($hash{$b}||=rand 1) } @_; } my @array = 1 .. 1000; cmpthese(-10, { slice => sub { () = slice @array }, xform => sub { () = xform @array }, shufl => sub { () = shufl @array }, qshuf => sub { () = qshuf @array }, lutil => sub { () = shuffle @array }, }); __END__ Rate qshuf xform slice shufl lutil qshuf 90.7/s -- -12% -36% -66% -97% xform 103/s 13% -- -27% -61% -96% slice 141/s 55% 37% -- -47% -95% shufl 264/s 191% 157% 87% -- -91% lutil 2870/s 3066% 2694% 1939% 988% --
Not a winner. Not anywhere near the winner either.

In reply to Re^5: Is this a fair shuffle? by Anonymous Monk
in thread Is this a fair shuffle? by Roy Johnson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.