On Tue, Jan 09, 2001 at 12:50:08PM +, Piers Cawley wrote:
> David Cantrell <[EMAIL PROTECTED]> writes:
> > Also index. These two snippets are equivalent:
> > if($foo=~/foo/) { ... }
> > if(index($foo, 'foo')!=-1) { ... }
> > I always want to do just plain if(index(...)) though.
>
> ISTR that (for weird reasons), the regex version of that is faster.
If it is, it appears to be hidden by the other code ...
beta:~ $ perl match-cmp
Name "main::words" used only once: possible typo at match-cmp line 5.
Benchmark: running index, index_, regex, regex_, each for at least 10 CPU seconds...
index: 19 wallclock secs (18.71 usr + 0.00 sys = 18.71 CPU) @ 1.50/s (n=28)
index_: 19 wallclock secs (19.41 usr + 0.00 sys = 19.41 CPU) @ 1.49/s (n=29)
regex: 19 wallclock secs (19.11 usr + 0.01 sys = 19.12 CPU) @ 1.36/s (n=26)
regex_: 19 wallclock secs (18.62 usr + 0.00 sys = 18.62 CPU) @ 1.50/s (n=28)
beta:~ $ cat match-cmp
#!/usr/bin/perl -w
use Benchmark;
push @ARGV, '/usr/dict/words';
@words = <>;
timethese( -10, {
regex => 'foreach $word (@words) { $word =~ /foo/ }',
regex_ => 'foreach (@words) { /foo/ }',
index => 'foreach $word (@words) { index $word, "foo" }',
index_ => 'foreach (@words) { index $_, "foo" }',
}
);
--
Chris Benson