in reply to 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).sub shuffle { my @a=@_; my $n; my $ret; my $i=@_; map { $n = rand($i--); $ret = $a[$n]; $a[$n] = $a[$i]; $ret; } @_; }
Update: Aha. It's large data being shuffled, not large lists, that makes List::Util the better answer in some instances. Thanks ikegami.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: About List::Util's pure Perl shuffle()
by ikegami (Patriarch) on Jul 11, 2007 at 17:47 UTC | |
by blazar (Canon) on Jul 12, 2007 at 09:58 UTC | |
by ikegami (Patriarch) on Jul 12, 2007 at 13:29 UTC | |
Re^2: About List::Util's pure Perl shuffle()
by blazar (Canon) on Jul 11, 2007 at 23:13 UTC | |
by runrig (Abbot) on Jul 11, 2007 at 23:27 UTC | |
by ikegami (Patriarch) on Jul 12, 2007 at 01:12 UTC | |
by BrowserUk (Patriarch) on Jul 12, 2007 at 00:32 UTC |
In Section
Meditations