in reply to Array Shuffle

List::Util's shuffle is a little faster still. Adding:

my $sub3 = sub { shuffle (@randomarray); };

and changing timethese to cmpthese (with other changes as required) gives:

Rate Sub 2 Sub 1 Sub 32 Sub 2 14869/s -- -13% -89% Sub 1 17066/s 15% -- -87% Sub 32 131573/s 785% 671% --

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Array Shuffle
by ikegami (Patriarch) on Jun 11, 2007 at 14:43 UTC
    Isn't List::Util's shuffle implemented in C/XS? I'd be more interested in seeing a comparion where both algorithms were implemented in C/XS (or where both algorithms were implemented in Perl).
      ikegami,
      Well, yes. It falls back to a pure perl version if the XS version isn't available. This is unlikely to happen since List::Util is part of the core but there is nothing preventing you from using the pure perl version as part of the benchmark.

      Cheers - L~R

      I understand your intellectual curiosity wrt the algorithm, but my post was more a subtle hint that using a pre-packaged wheel is often a better way of spending time than polishing your own variant. Seems from OP's reply I picked the right answer.

      From the benchmark results I guessed XS was likely lurking in the background somewhere, but in this case that can be treated as a reasonable optimisation by the module author. There is no hint in the List::Util docs that indicate the implementation method, however the guts of the routine seems to be:

      for (index = items ; index > 1 ; ) { int swap = (int)(Drand01() * (double)(index--)); SV *tmp = ST(swap); ST(swap) = ST(index); ST(index) = tmp; } XSRETURN(items);

      DWIM is Perl's answer to Gödel
Re^2: Array Shuffle
by logie17 (Friar) on Jun 11, 2007 at 15:30 UTC
    GrandFather, Thank you for the reference to List::Util. I was actually just seeking a way to improve my current algorithms. (To squeeze as much out of Perl as I possibly could for my own personal edification). I would curious ot know if List Util using C to gain the edge?

    Thanks again
    s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;