my @array = ...; ... my @shuffled = shuffle @array; #### my @array = ... ... my @shuffled = shuffle \@array; #### my @shuffled = shuffle ... some expression generating a list ...; #### my @shuffled = shuffle [ ... some expression generating a list ... ]; #### 10 strings length 10 Rate naive listutil blokhead_ref buk blokhead bukNew naive 20559/s -- -44% -69% -70% -70% -74% listutil 36857/s 79% -- -44% -47% -47% -53% blokhead_ref 66375/s 223% 80% -- -4% -5% -16% buk 69370/s 237% 88% 5% -- -0% -12% blokhead 69635/s 239% 89% 5% 0% -- -12% bukNew 79050/s 285% 114% 19% 14% 14% -- 10 strings length 1000 Rate naive listutil blokhead_ref blokhead buk bukNew naive 1266/s -- -60% -98% -98% -98% -98% listutil 3164/s 150% -- -95% -95% -95% -96% blokhead_ref 67220/s 5209% 2024% -- -1% -3% -16% blokhead 67878/s 5261% 2045% 1% -- -2% -15% buk 69084/s 5356% 2083% 3% 2% -- -14% bukNew 80288/s 6241% 2437% 19% 18% 16% -- 100 strings length 1000 Rate naive listutil buk blokhead blokhead_ref bukNew naive 93.7/s -- -58% -99% -99% -99% -99% listutil 225/s 141% -- -97% -97% -97% -98% buk 7540/s 7946% 3244% -- -5% -7% -23% blokhead 7919/s 8349% 3412% 5% -- -2% -19% blokhead_ref 8105/s 8547% 3494% 7% 2% -- -18% bukNew 9831/s 10390% 4260% 30% 24% 21% -- 1000 strings length 1000 Rate naive listutil buk blokhead blokhead_ref bukNew naive 8.16/s -- -63% -99% -99% -99% -99% listutil 22.1/s 170% -- -97% -97% -97% -97% buk 737/s 8932% 3240% -- -8% -10% -11% blokhead 804/s 9750% 3542% 9% -- -2% -3% blokhead_ref 819/s 9927% 3608% 11% 2% -- -1% bukNew 826/s 10023% 3643% 12% 3% 1% -- #### #!/usr/bin/perl -slw use strict; use List::Util qw[ shuffle ]; use Benchmark qw/:all/; sub naive (@) { my @l=@_; for (reverse 1..$#l) { my $r=int rand($_+1); @l[$_,$r]=@l[$r,$_]; } @l; } sub listutil (@) { my @a=\(@_); my $n; my $i=@_; map { $n = rand($i--); (${$a[$n]}, $a[$n] = $a[$i])[0]; } @_; } sub buk (@) { my @a = \( @_ ); my $n; my $i = @_; map+( $n = rand($i--), ${ $a[ $n ] }, $a[ $n ] = $a[ $i ] )[ 1 ], @_; } sub bukNew ($) { my( $ref ) = @_; my @x = 0 .. $#$ref; @{ $ref }[ map splice( @x, rand @x, 1 ), @x ]; } #my %stats; #++$stats{ join' ', bukNew( ['A'..'D'] ) } for 1 .. 1e4; #print "$_ => $stats{ $_ }" for sort keys %stats; sub blokhead (@) { my @a = (0 .. $#_); my $i = @_; my $n; map+( $n=rand($i--), $_[$a[$n]], $a[$n]=$a[$i] )[ 1 ], @_; } sub blokhead_ref ($) { my( $ref ) = @_; my @a = (0 .. $#$ref); my $i = @$ref; my $n; map+( $n=rand($i--), $ref->[$a[$n]], $a[$n]=$a[$i] )[ 1 ], @a; } #%stats = (); #++$stats{ join' ', blokhead_ref( ['A'..'D'] ) } for 1 .. 1e4; #print "$_ => $stats{ $_ }" for sort keys %stats; for my $c ( map{ 10**$_ } 1..3 ) { for my $l ( map{ 10**$_ } 0.1, 1, 3 ) { print "\n$c strings length $l"; our @test = map $_ x $l, 1..$c; cmpthese -3, { bukNew => q[ bukNew( \@test ) ], blokhead => q[ blokhead_ref( \@test ) ], map { $_ => "$_ \@test" } qw/naive listutil blokhead buk/ }; } }