### 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??

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], @_;
}
[download]

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%   --
[download]

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 ], @_;
}
[download]

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.

Create A New User
Node Status?
node history
Node Type: note [id://626103]
help
Chatterbox?
 [Corion]: thezip: start "some title" path\to\that\ application, but that will open another console window [Corion]: thezip: If you want to confuse your users, use system(1, "that\\command" );, which will make Perl launch it in the background [Corion]: That will keep the console window open even though the user can't type into it anymore [thezip]: So I have a script that generates a log file. After script completion, I want tohave VIM open this logfile. [thezip]: i don't get the command line "back" until I close VIM. No what I want to happen... [thezip]: I currently don't have access to CYGWIN, else I'd just do a tail -f on the logfile. [Corion]: thezip: If you want to open vim and can live with opening a second console window, use start "The results" vim.exe c:\path\to\logfile .log

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2017-03-27 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (321 votes). Check out past polls.