Re: Speed comparison of regex versus index, lc, and / /i
- From: Ben Bullock <benkasminbullock@xxxxxxxxx>
- Date: Sat, 31 May 2008 00:16:18 +0000 (UTC)
On Fri, 30 May 2008 14:55:04 +0000, xhoster wrote:
Ben Bullock <benkasminbullock@xxxxxxxxx> wrote:
In a recent discussion on this newsgroup, it was mentioned that "index"
is better for matching fixed strings than using regular expressions.
Yes, it is.
Proof?
If using regex to match fixed strings, you need to worry
about special characters or syntax errors in the regex, like the problem
with the literal string like "[l-c]" which we recently witnessed here.
That was exactly the discussion I was referring to. I said to use a
regex with \Q and \E, and Jurgen Exner said that "index" was the
correct tool for the job. I'm continuing that discussion on a separate
thread, since some time has passed and this is a separate topic from
that. I think Mr Exner was incorrect.
"Better" is a much bigger issue than merely faster.
OK, what is your definition of "better"?
And of course, if you are interested in where the string matches (i.e.
the return value of index, and not just whether or not it is -1) then it
is simpler to get it from index than from a regex.
Really? Please edit the following to show me how:
#!/usr/local/bin/perl
use warnings;
use strict;
sub index_find
{
my ($text, $ss) = @_;
my @finds;
my $found = 0;
while (1) {
$found = index ($text, $ss, $found);
last if $found == -1;
push @finds, $found;
$found += length ($ss);
}
return \@finds;
}
sub regex_find
{
my ($text, $ss) = @_;
my @finds;
while ($text =~ /\Q$ss\E/g) {
push @finds, pos ($text) - length($ss);
}
return \@finds;
}
my $text = <<EOF;
xhoster is the coolest perl programmer ever. xhoster is the
greatest. xhoster is the champion. xhoster is a babe magnet.
EOF
my $ss = "xhoster";
for (\&index_find, \®ex_find) {
print "String found at ", (join ", ",@{&{$_}($text, $ss)}),"\n";
}
It's possible to reduce the while (1) in the first line to something like
while (($found = index ($text, $ss, $found)) != -1) {
of course, but that doesn't make it simpler.
.
- Follow-Ups:
- Re: Speed comparison of regex versus index, lc, and / /i
- From: A. Sinan Unur
- Re: Speed comparison of regex versus index, lc, and / /i
- References:
- Speed comparison of regex versus index, lc, and / /i
- From: Ben Bullock
- Re: Speed comparison of regex versus index, lc, and / /i
- From: xhoster
- Speed comparison of regex versus index, lc, and / /i
- Prev by Date: Re: The Importance of Terminology's Quality
- Next by Date: Re: Using perl locally on a Windows XP system
- Previous by thread: Re: Speed comparison of regex versus index, lc, and / /i
- Next by thread: Re: Speed comparison of regex versus index, lc, and / /i
- Index(es):
Relevant Pages
|
|