Your solution is not guaranteed to be well shuffled since you relying on sort to shuffle ties.

By using sort, you've converted a O(N) problem into an O(N log N) problem. That can make a substantial difference in execution time. In other words, your solution is less scalable.

Something which isn't scalable could still be fast for smaller input set, but it's not even the case here:

>perl script.pl 50 Rate mrborisguy ikegami mrborisguy 2074/s -- -51% ikegami 4225/s 104% -- # Without XS. Rate mrborisguy ikegami mrborisguy 2167/s -- -91% ikegami 23688/s 993% -- # With XS.

And for an array of 1700, as relevant here:

>perl script.pl 1700 Rate mrborisguy ikegami mrborisguy 31.0/s -- -74% ikegami 120/s 286% -- # Without XS. Rate mrborisguy ikegami mrborisguy 31.7/s -- -96% ikegami 722/s 2179% -- # With XS.

The benchmark script:

use strict; use warnings; use Benchmark (); use List::Util (); sub mrborisguy { my @shuffled = map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [rand(),$_] } @{$_[0]}; } sub ikegami { my @shuffled = List::Util::shuffle(@{$_[0]}); } { my @data = map { [] } 1..$ARGV[0]; Benchmark::cmpthese(-3, { mrborisguy => sub { mrborisguy \@data }, ikegami => sub { ikegami \@data }, }); }

Update: Added benchmarks to back my statements.


In reply to Re^2: Randomising the order of an array by ikegami
in thread Randomising the order of an array by g0n

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.