Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

In a recent discussion (link @ GG) in ctt, PerlTeX was mentioned (and I'm also mentioning it here because it is not that widely known and IMHO it would deserve being) in relation to shuffling, at which point in reply to a posted piece of code I wrote about the perldoc -q shuffle FAQ entry, talking in particular about both Fisher-Yates and List::Util's shuffle(). Now, I happened to check the source code for the latter module, and since it has pure Perl code for the given function, I noticed that in particular it is as follows:

sub shuffle (@) { my @a=\(@_); my $n; my $i=@_; map { $n = rand($i--); (${$a[$n]}, $a[$n] = $a[$i])[0]; } @_; }

Now, at first sight it is a nice piece of Obfu, ain't it? Well, then if you look at it, it's easy to see how it works and in particular that it is still Fisher-Yates. But of course you have to think about it for a while... and I wondered why it is like that...

To be fair, I think that taking references in the first place is to avoid duplicating data in memory that could be heavy in memory usage... but then I also thought that all that playing with references would impose a performance penalty and -for once- I suppose that performance does matter. So I tried the following Benchmark, comparing a naive and fairly readable implementation of the algorithm with List::Util's:

#!/usr/bin/perl use strict; use warnings; use Benchmark qw/:all :hireswallclock/; sub naive (@) { my @l=@_; for (reverse 1..$#l) { my $r=int rand($_+1); @l[$_,$r]=@l[$r,$_]; } @l; } sub listutil (@) { my @a=\(@_); my $n; my $i=@_; map { $n = rand($i--); (${$a[$n]}, $a[$n] = $a[$i])[0]; } @_; } cmpthese -60, { map { $_ => "$_ 1..1000" } qw/naive listutil/ }; __END__

The results are as follows:

C:\temp>perl lus.pl Rate naive listutil naive 588/s -- -14% listutil 684/s 16% --

So that pretty much may answer my question as to why the sub is implemented like that... but then this raises the question as to how could one come up with such an idea, because I'm sure I wouldn't have...

Any comments?


In reply to About List::Util's pure Perl shuffle() by blazar

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (13)
    As of 2014-07-31 18:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (251 votes), past polls