tomhukins has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 1: Quickly finding an element in an array
by tilly (Archbishop) on Jan 08, 2002 at 02:37 UTC | |
|
Re: Quickly finding an element in an array
by blakem (Monsignor) on Jan 08, 2002 at 01:22 UTC | |
by tomhukins (Curate) on Jan 08, 2002 at 01:48 UTC | |
by blakem (Monsignor) on Jan 08, 2002 at 01:56 UTC | |
|
Re: Quickly finding an element in an array
by robin (Chaplain) on Jan 08, 2002 at 01:37 UTC | |
|
Re: Quickly finding an element in an array
by Juerd (Abbot) on Jan 08, 2002 at 13:49 UTC |