Dear Monks, I recently read this post on shuffling arrays. I put together a couple other methods of shuffling using the Knuth shuffle.
#! /usr/local/bin/perl use strict; use warnings; my @randomarray = ( 0 .. 100 ); my $sub1 = sub { for(my $j, my $x, my $i = scalar @randomarray; $i;$j = int(ran +d($i)), $x = $randomarray[--$i], $randomarray[$i] = $randomarray[$j], + $randomarray[$j] = $x){ } }; my $sub2 = sub { my $i = scalar @randomarray; while ($i){ my $j = int(rand($i)); my $x = $randomarray[--$i]; #next value $randomarray[$i] = $randomarray[$j]; #next value = ran +dom value $randomarray[$j] = $x; #current random value = next va +lue. } }; use Benchmark; timethese(100000, { 'Sub 1' => $sub1, 'Sub 2' => $sub2, });
I was wondering if I could get a little help to obfuscate the above benchmarks a little more? In true Perl style, such as perhaps using map or other better ways to iterate through the algorithm to help it gain even a better edge. Here are my benchmark results:
Benchmark: timing 100000 iterations of Sub 1, Sub 2... Sub 1: 28 wallclock secs (27.37 usr + 0.00 sys = 27.37 CPU) @ 36 +54.01/s (n=100000) Sub 2: 40 wallclock secs (32.25 usr + 0.00 sys = 32.25 CPU) @ 31 +00.78/s (n=100000)

It looks like using the For loop is a bit faster. Thank you for any comments in advance.

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

In reply to Array Shuffle by logie17

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.