Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: About List::Util's pure Perl shuffle()

by blazar (Canon)
on Jul 11, 2007 at 23:13 UTC ( #626103=note: print w/ replies, xml ) Need Help??


in reply to Re: About List::Util's pure Perl shuffle()
in thread About List::Util's pure Perl shuffle()

I suppose taking references might save memory, but it doesn't save any time over this (on small or large lists):
[snip]
I get about 16% better than the List::Util version (YMMV).

Indeed I was about to yell: "well, but then let's just adapt BrowserUk's version simply removing the referencing and dereferencing":

sub bzr (@) { my @a = @_; my $n; my $i = @_; map +($n=randi($i--), $a[$n], $a[$n]=$a[$i])[1], @_; }

But that won't work any more! (So kids don't copy the code above, it's crap! ;-) OTOH if I compare your code (as 'run') with BrowserUk's, I get:

Rate run buk run 7467/s -- -12% buk 8454/s 13% --

So that seems the best both speed and possibly memory wise.


Comment on Re^2: About List::Util's pure Perl shuffle()
Select or Download Code
Re^3: About List::Util's pure Perl shuffle()
by runrig (Abbot) on Jul 11, 2007 at 23:27 UTC
    That's strange, I compared mine against BrowserUK's and get that they are very nearly equal (mine ahead by a meaningless 1 to 4%), until the list gets large (around a million), then I'm ahead by ~10%. This is for lists containing data only a few characters long, but as ikegami points out, for larger data, BrowserUK's wins by a longshot.
Re^3: About List::Util's pure Perl shuffle()
by BrowserUk (Pope) on Jul 12, 2007 at 00:32 UTC

    Here's a non-aliasing version that does work, but it doesn't perform very well. Why it works is left as an exercise.

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

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: About List::Util's pure Perl shuffle()
by ikegami (Pope) on Jul 12, 2007 at 01:12 UTC
    Ah, but the fix is easy. It's in my reply (which was posted and updated with the fix before you posted yours).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://626103]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-12-25 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls