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

In reply to Re: Loops, lists and efficiency by davido
in thread Loops, lists and efficiency by Arunbear

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.