in reply to Re: referring to anonymous array in value of a hash in array context...
in thread referring to anonymous array in value of a hash in array context...
Actually the size of the array shouldn't matter because when foreach gets a real array perl will be smart and won't expand things in place (it uses an iterator over the AV* behind the scenes which all happens in C underneath).
$ perl -MBenchmark=timethese -e <<'EOC' $a = [ 0 ..5_000 ]; timethese( 2_000, { index => sub { for( my $i = 0; $i <= $#{ $a }; $i++ ) { $b = $a->[ +$i] } }, iterator => sub { for( @{ $a } ) { $b = $_ } } } ) EOC Benchmark: timing 2000 iterations of index, iterator... index: 27 wallclock secs (21.21 usr + 0.02 sys = 21.23 CPU) @ 94 +.21/s (n=2000) iterator: 6 wallclock secs ( 5.39 usr + 0.00 sys = 5.39 CPU) @ 37 +1.06/s (n=2000)
|
|---|