If I want to find out if an element is in an array I can use grep. If I want to find several elements, I can convert the array to a hash and use exists.

I can also use a foreach loop and iterate over the elements, calling last if the element is found. If the element it towards the start of the array, this makes more sense than iterating over the whole array, as the other two methods do.

I decided to benchmark these different techniques, matching the number 0 at the start, middle and end of the array. As I'd expected, foreach is best for arrays with 0 at the start because of the last call, but grep is best in general. Of course, if I were matching multiple elements, I'd create a hash.

So, now for my question: Is this any way I can combine the benefits of the grep and foreach methods by using last within a grep? I've tried doing this, but last doesn't work within grep. Any ideas?

For those who are interested, here is the benchmarking code I used and its results:

#!/usr/bin/perl -Tw use strict; use Benchmark; my @array; foreach (1..1000) { push @array, 1 + int rand 199; } my @start = (0, @array); my @end = (@array, 0); my @middle = @array; splice(@middle, 500, 0, 0); timethese(1000, { 'grep_start' => sub { my $found = 0; my @list = grep(0, @start); $found = 1 if scalar @list; }, 'foreach_start' => sub { my $found = 0; foreach (@start) { $_ == 0 and $found = 1 and last; } }, 'hash_start' => sub { my $found = 0; my %hash = map { $_ => undef } @start; $found = 1 if exists $hash{0}; }, 'grep_end' => sub { my $found = 0; my @list = grep(0, @end); $found = 1 if scalar @list; }, 'foreach_end' => sub { my $found = 0; foreach (@end) { $_ == 0 and $found = 1 and last; } }, 'hash_end' => sub { my $found = 0; my %hash = map { $_ => undef } @end; $found = 1 if exists $hash{0}; }, 'grep_middle' => sub { my $found = 0; my @list = grep(0, @middle); $found = 1 if scalar @list; }, 'foreach_middle' => sub { my $found = 0; foreach (@middle) { $_ == 0 and $found = 1 and last; } }, 'hash_middle' => sub { my $found = 0; my %hash = map { $_ => undef } @middle; $found = 1 if exists $hash{0}; } });

The results were as follows:
Benchmark: timing 1000 iterations of foreach_end, foreach_middle, foreach_start, grep_end, grep_middle, grep_start, hash_end, hash_middle, hash_start...
foreach_end: 1 wallclock secs ( 1.03 usr + 0.01 sys = 1.04 CPU)
foreach_middle: 1 wallclock secs ( 0.52 usr + 0.00 sys = 0.52 CPU)
foreach_start: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU) (warning: too few iterations for a reliable count)
grep_end: 0 wallclock secs ( 0.47 usr + 0.00 sys = 0.47 CPU)
grep_middle: 1 wallclock secs ( 0.47 usr + 0.00 sys = 0.47 CPU)
grep_start: 1 wallclock secs ( 0.48 usr + 0.00 sys = 0.48 CPU)
hash_end: 15 wallclock secs (14.26 usr + 0.00 sys = 14.26 CPU)
hash_middle: 15 wallclock secs (14.25 usr + 0.00 sys = 14.25 CPU)
hash_start: 15 wallclock secs (14.09 usr + 0.03 sys = 14.12 CPU)

Edit Petruchio Thu Jan 10 11:47:48 UTC 2002 - Replaced tabs with spaces, at the author's request. Just say no to tabbed indentation!


In reply to Quickly finding an element in an array by tomhukins

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.