in reply to Re: Randomising the order of an array
in thread Randomising the order of an array
Your solution is not guaranteed to be well shuffled since you relying on sort to shuffle ties.
By using sort, you've converted a O(N) problem into an O(N log N) problem. That can make a substantial difference in execution time. In other words, your solution is less scalable.
Something which isn't scalable could still be fast for smaller input set, but it's not even the case here:
>perl script.pl 50 Rate mrborisguy ikegami mrborisguy 2074/s -- -51% ikegami 4225/s 104% -- # Without XS. Rate mrborisguy ikegami mrborisguy 2167/s -- -91% ikegami 23688/s 993% -- # With XS.
And for an array of 1700, as relevant here:
>perl script.pl 1700 Rate mrborisguy ikegami mrborisguy 31.0/s -- -74% ikegami 120/s 286% -- # Without XS. Rate mrborisguy ikegami mrborisguy 31.7/s -- -96% ikegami 722/s 2179% -- # With XS.
The benchmark script:
use strict; use warnings; use Benchmark (); use List::Util (); sub mrborisguy { my @shuffled = map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [rand(),$_] } @{$_[0]}; } sub ikegami { my @shuffled = List::Util::shuffle(@{$_[0]}); } { my @data = map { [] } 1..$ARGV[0]; Benchmark::cmpthese(-3, { mrborisguy => sub { mrborisguy \@data }, ikegami => sub { ikegami \@data }, }); }
Update: Added benchmarks to back my statements.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Randomising the order of an array
by Anonymous Monk on Jul 19, 2005 at 20:03 UTC | |
by ikegami (Patriarch) on Jul 19, 2005 at 20:07 UTC | |
by Anonymous Monk on Jul 19, 2005 at 20:14 UTC |