Click here to get back home

FAQ 6.21 What's wrong with using grep in a void context?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 6.21 What's wrong with using grep in a void context? PerlFAQ Server 03-20-2008
Posted by PerlFAQ Server on March 20, 2008, 9:03 am
Please log in for more thread options
This is an excerpt from the latest version perlfaq6.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

6.21: What's wrong with using grep in a void context?


The problem is that grep builds a return list, regardless of the
context. This means you're making Perl go to the trouble of building a
list that you then just throw away. If the list is large, you waste both
time and space. If your intent is to iterate over the list, then use a
for loop for this purpose.

In perls older than 5.8.1, map suffers from this problem as well. But
since 5.8.1, this has been fixed, and map is context aware - in void
context, no lists are constructed.



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Posted by szr on March 20, 2008, 11:57 pm
Please log in for more thread options
PerlFAQ Server wrote:

> 6.21: What's wrong with using grep in a void context?
>
>
> The problem is that grep builds a return list, regardless of the
> context. This means you're making Perl go to the trouble of
> building a list that you then just throw away. If the list is
> large, you waste both time and space. If your intent is to iterate
> over the list, then use a for loop for this purpose.
>
> In perls older than 5.8.1, map suffers from this problem as well.
> But since 5.8.1, this has been fixed, and map is context aware -
> in void context, no lists are constructed.

So why not simply make grep context aware too? Seems grep would benefit
from this just as much, if not more, as map.

Thoughts?

--
szr



Posted by benkasminbullock@gmail.com on March 21, 2008, 12:37 am
Please log in for more thread options

> So why not simply make grep context aware too? Seems grep would benefit
> from this just as much, if not more, as map.

I agree that if it's easy to make grep context aware, then they might
as well. For example if "grep" is called in void context Perl could
automatically replace the call to "grep" with one to "map".

But as far as I know, "grep" wouldn't benefit from this. I think
"grep" is exactly the same thing as "map" except for its return
values. The difference in the return values is that "grep" returns a
list of values where the evaluated expression or block is true,
whereas "map" returns all the values. The side-effects of "grep" and
"map" are identical. Thus calling "grep" in a void context has exactly
the same effect as calling "map", and the FAQ entry is just saying if
you are going to ignore the return value, there is a penalty to using
"grep", so you should have used "map".

Posted by szr on March 21, 2008, 4:10 am
Please log in for more thread options
benkasminbullock@gmail.com wrote:
>
>> So why not simply make grep context aware too? Seems grep would
>> benefit from this just as much, if not more, as map.
>
> I agree that if it's easy to make grep context aware, then they might
> as well. For example if "grep" is called in void context Perl could
> automatically replace the call to "grep" with one to "map".
>
> But as far as I know, "grep" wouldn't benefit from this. I think
> "grep" is exactly the same thing as "map" except for its return
> values. The difference in the return values is that "grep" returns a
> list of values where the evaluated expression or block is true,
> whereas "map" returns all the values. The side-effects of "grep" and
> "map" are identical. Thus calling "grep" in a void context has exactly
> the same effect as calling "map", and the FAQ entry is just saying if
> you are going to ignore the return value, there is a penalty to using
> "grep", so you should have used "map".

I get that, though I think there may be some cases where one might want
to use grep and it's filtering to compare the 'size' it returns to the
original size of the array.

For example:

my @a1 = ( [qw/A B C/], [qw/D E F/], [qw/G H I/] );
my @a2 = ( [qw/A B C/], [qw/D E F/], [qw/G H I/] );

my $match =
@a1 == grep { my $i = $_;
@ == grep {
$a1[$i][$_] eq $a2[$i][$_];
} (0..$#)
} (0..$#a1);

print $match;


I don't see why there should be a performance penalty in this example,
if it was avoided with map. Making grep context aware would be real nice
if you change the arrays to have 100_000 elements each.

--
szr



Posted by brian d foy on March 21, 2008, 6:46 pm
Please log in for more thread options
wrote:


> I get that, though I think there may be some cases where one might want
> to use grep and it's filtering to compare the 'size' it returns to the
> original size of the array.

If you're doing that, you don't have grep in void context.

Similar ThreadsPosted
FAQ 6.21: What's wrong with using grep in a void context? November 23, 2004, 12:03 am
FAQ 6.21 What's wrong with using grep in a void context? January 23, 2005, 6:03 pm
FAQ 6.21 What's wrong with using grep in a void context? May 16, 2005, 5:03 pm
FAQ 6.21 What's wrong with using grep in a void context? August 1, 2005, 4:03 am
FAQ 6.21 What's wrong with using grep in a void context? October 10, 2005, 10:03 pm
FAQ 6.21 What's wrong with using grep in a void context? November 28, 2005, 11:03 am
FAQ 6.21 What's wrong with using grep in a void context? January 17, 2006, 5:03 pm
FAQ 6.21 What's wrong with using grep in a void context? May 4, 2006, 3:03 am
FAQ 6.21 What's wrong with using grep in a void context? September 1, 2006, 9:03 pm
FAQ 6.21 What's wrong with using grep in a void context? November 11, 2006, 9:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap