Thanks, I've modified my code to use this, but surprisingly perl is dumping core (5.005_03 on FreeBSD and 5.6.1 on Solaris). This is a different problem, but I'm really surprised it's happening. Does anyone know why? Here's some code that reproduces the problem:
#!/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_middle' => sub { my $found = 0; grep {$_ == 0 and $found = 1 and last} @middle; }, '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}; } });
In reply to Re: Re: Quickly finding an element in an array
by tomhukins
in thread Quickly finding an element in an array
by tomhukins
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |