#! /usr/local/bin/perl use strict; use warnings; my @randomarray = ( 0 .. 100 ); my $sub1 = sub { for(my $j, my $x, my $i = scalar @randomarray; $i;$j = int(rand($i)), $x = $randomarray[--$i], $randomarray[$i] = $randomarray[$j], $randomarray[$j] = $x){ } }; my $sub2 = sub { my $i = scalar @randomarray; while ($i){ my $j = int(rand($i)); my $x = $randomarray[--$i]; #next value $randomarray[$i] = $randomarray[$j]; #next value = random value $randomarray[$j] = $x; #current random value = next value. } }; use Benchmark; timethese(100000, { 'Sub 1' => $sub1, 'Sub 2' => $sub2, }); #### Benchmark: timing 100000 iterations of Sub 1, Sub 2... Sub 1: 28 wallclock secs (27.37 usr + 0.00 sys = 27.37 CPU) @ 3654.01/s (n=100000) Sub 2: 40 wallclock secs (32.25 usr + 0.00 sys = 32.25 CPU) @ 3100.78/s (n=100000)