Jay Savage wrote:
[snip]
>>
In any case, if someone offered me a way of making my program run
in 20ms instead of 25ms I wouldn't be overly impressed, and
certainly don't see it as a case of grep 'shining'.

I think you missed my point. I may not have been clear. No, shaving a
few ms off runtime isn't shining. But it's an *example* of the *type*
of thing grep shines at. That is, taking loops and tunring them into
blocks, and cutting out, in this case, 999 system calls. Are the
savings anything to write home about? No, of course not. But the
*idea* is fantastic, and in a more complicated situation often leads
to not just faster execution, but saved programmer time, increased
clarity, and simplified control structures.

Then as far as I can tell we concur!

I thought your point was that

  print join "\n", @list, '';

was superior to

  print "$_\n" foreach @list;

whereas you meant something more like

  process_list(grep { $_ % 2 == 0 } @list);

is superior to

  my @temp;
  foreach (@list) {
    push @temp, $_ if $_ % 2 == 0;
  }
  process_list(@temp);

with which I would wholeheartedly agree :)

I would say though that it is a shame that it was called grep. The
utility is far from universally known, and the function doesn't provide
a Global Regular Expression Print facility. I prefer to see it as a list
mapping function: given an input list it returns an output list which is
a function of the input. It can even be emulated with the map function.

Again, it's not about a few prints in this particular made-up case.
It's about the types of things grep really does well.

 My guess is that optimising out print calls would be less, not more
 effective than with most subroutines: all it does is to append the
 passed-in data to the output buffer, and the system call is performed
 only when that buffer is filled.


Well, that depends entirely on what file handle you're printing to. If
you're printing to STDOUT, as all these examples so far have, each
newline--and therefore each pass though the loop that matches the
condition--flushes the buffer.
>
[snip]

Now there I would disagree. Only STDERR is autoflushed: data sent to
STDOUT is output only when the buffer fills, unless that is explicitly
changed.

Cheers,

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to