"Sean M. Burke" <[EMAIL PROTECTED]> schrieb im Newsbeitrag
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
| I was looking at the implementation of the FY shuffle that I see in
| Perlfaq4, namely:
[...]
| This is the benchmarker code:
[...]
| And neither implementation seems to do anything crazy, like going into an
| infinite loop when given a one-element list, nor always returning ('b',
| 'a') when given ('a', 'b').
Hmm. Same goes for this implementation which lacks the explicit int and is
still a little faster.
Size of deck: 1000
Benchmark: timing 10000 iterations of way2, way3...
way2: 59 wallclock secs
(50.26 usr + 0.01 sys = 50.27 CPU) @ 198.92/s (n= 10000)
way3: 50 wallclock secs
(43.01 usr + 0.01 sys = 43.02 CPU) @ 232.44/s (n=10000)
Size of deck: 100
Benchmark: timing 100000 iterations of way2, way3...
way2: 52 wallclock secs
(49.50 usr + 0.05 sys = 49.55 CPU) @ 2018.08/s (n=100000)
way3: 46 wallclock secs
(42.99 usr + 0.01 sys = 43.00 CPU) @ 2325.47/s (n=100000)
use strict;
use warnings;
use Benchmark;
my @x; for(1 .. 1000) { push @x, rand(1_000_000) }
print "Size of deck: ", scalar(@x), "\n";
timethese(10000, {
'way2' => sub { fy2(\@x) },
'way3' => sub { fy3(\@x) },
});
splice @x, 100;
print "Size of deck: ", scalar(@x), "\n";
timethese(100000, {
'way2' => sub { fy2(\@x) },
'way3' => sub { fy3(\@x) },
});
sub fy2 {
my($deck,$i,$j,$t) = $_[0];
$i = @$deck || return;
while(1) {
$j = int rand($i-- || return);
$t = $deck->[$i];
$deck->[$i] = $deck->[$j];
$deck->[$j] = $t;
}
}
sub fy3 {
my($deck,$i,$j,$t) = $_[0];
$i = @$deck || return;
while(1) {
$j = rand($i-- || return);
$t = $deck->[$i];
$deck->[$i] = $deck->[$j];
$deck->[$j] = $t;
}
}
Best regards,
Steffen M�ller
--
$_=qq#tsee gmx.net#;s#e#s#g;s#[^\s\w]#c#;s#s#ust#g;s#t#J#e;s#nus#ker#
;chop;$c=' ^^^^ ';$c='12319';@c=split/(..)/,'8234130006710523';@d=split"3"
,$c;chop;' at ';s#(t)ustust#$1\0ano$1;.#;y#.; #ehr#;@_=$_;shift@c,substr
$_[0],$_,1,chr(ord(substr$_[0],$_)-shift@c)for$d[0]..$d[1];print"$_[0]\n";