The bottleneck has nothing to do with whether you use grep or not. The print function slows things down so much that you can't detect the difference between the two snippets you've provided when using Benchmark.
That said, I removed the print and put a simple $var++ in its place to avoid a no-op. And I ran them through Benchmark (it's easy, really). Here's the outcome:
use strict;
use warnings;
use Benchmark;
sub fortest {
my $number = 0;
for(1 .. 100) {
$number++ if $_ < 50;
}
}
sub greptest {
my $number = 0;
for(grep {$_ < 50} (1 .. 100)) {
$number++;
}
}
my $count = 100000;
timethese( $count, {
'for' => \&fortest,
'grep' => \&greptest
} );
__OUTPUT__
Benchmark: timing 100000 iterations of for, grep...
for: 5 wallclock secs ( 4.85 usr + 0.02 sys = 4.87 CPU) @ 20546.5
+4/s (n=100000)
grep: 6 wallclock secs ( 5.16 usr + 0.00 sys = 5.16 CPU) @ 19387.
+36/s (n=100000)
It's that easy. And as you can see, the grep method is slightly slower over 100000 iterations. You would probably get a more meaningful time differential if you added iterations to the inner loops.
I had to clean up your second question a little before I could do anything with it. I added lexical scoping ('my' declarations), and removed the print function again, replacing it with a simple autoincrement. It would probably have been better if I had incremented $_, but this will give the general idea. Here's the outcome:
use strict;
use warnings;
use Benchmark;
sub perlish_for {
my $iterator = 0;
my $array_ref = [1 .. 10000];
for(@$array_ref) {
$iterator++;
}
}
sub cish_for {
my $iterator = 0;
my $array_ref = [1 .. 10000];
for(my $i=0; $_ = $array_ref->[$i] ;$i++) {
$iterator++;
}
}
my $count = 1000;
timethese( $count, {
'Perlish' => \&perlish_for,
'C-ish' => \&cish_for
} );
__OUTPUT__
Benchmark: timing 1000 iterations of C-ish, Perlish...
C-ish: 9 wallclock secs ( 8.42 usr + 0.02 sys = 8.44 CPU) @ 118.
+46/s (n=1000)
Perlish: 7 wallclock secs ( 6.67 usr + 0.06 sys = 6.73 CPU) @ 14
+8.61/s (n=1000)
Be sure to play with Benchmark yourself next time. It's easier to test it yourself than to ask one of us to do it for you. ...just a tip. ;) It beats waiting for us to get around to answering something that you could come up with in a fraction of the time.
Dave
"If I had my life to do over again, I'd be a plumber." -- Albert Einstein
|