I stand corrected. You are correct on your points. Sorry for the accusation. For those interested, here is the code and results from benchmarking.
code:
use Benchmark;
sub foreach1000()
{
my @x = (1..1000);
foreach(@x)
{
my $z = $_;
}
}
sub for1000()
{
my @x = (1..1000);
for(my $y = 0; $y < scalar(@x); $y++)
{
my $z = $x[$y];
}
}
sub foreach10000()
{
my @x = (1..10000);
foreach(@x)
{
my $z = $_;
}
}
sub for10000()
{
my @x = (1..10000);
for(my $y = 0; $y < scalar(@x); $y++)
{
my $z = $x[$y];
}
}
timethese( 10000, { for1000 => 'for1000()', foreach1000 => 'foreach100
+0()', for10000 => 'for10000()', foreach10000 => 'foreach10000()' });
results:
Benchmark: timing 10000 iterations of for1000, for10000, foreach1000,
+foreach10000...
for1000: 59 wallclock secs (53.75 usr + 0.03 sys = 53.78 CPU) @ 18
+5.95/s (n=10000)
for10000: 676 wallclock secs (537.40 usr + 0.23 sys = 537.63 CPU) @
+ 18.60/s (n=10000)
foreach1000: 33 wallclock secs (24.67 usr + 0.00 sys = 24.67 CPU) @ 4
+05.27/s (n=10000)
foreach10000: 293 wallclock secs (250.74 usr + 0.21 sys = 250.95 CPU)
+ @ 39.85/s (n=10000)