Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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):
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.

Replies are listed 'Best First'.
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 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).
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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://626103]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2018-03-19 04:49 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (232 votes). Check out past polls.