Re: strings vs regular expressions
- From: "Jon Skeet [C# MVP]" <skeet@xxxxxxxxx>
- Date: 11 Apr 2007 03:29:03 -0700
On Apr 11, 9:48 am, "Henning Krause [MVP - Exchange]"
<newsgroups_rem...@xxxxxxxxxxxxxxxxx> wrote:
but string.IndexOf has very bad implemention. If you want a fast string
search, look for a .NET implementation of the Boyer-Moore algorithm - this
is also used in regular expressions internals.
I wouldn't say that IndexOf has a "very bad" implementation. In *some*
cases it won't be as fast as doing the "pre-work" involved for Boyer-
Moore, but I suspect in the vast majority of cases used in the real
world, it's far quicker to use the "brute force" method, given that
you're only looking for the string once (as far as String.IndexOf is
concerned - you may be calling it multiple times, of course).
I suppose String.IndexOf could apply some heuristics and guess whether
it's worth building the tables (or whatever) for Boyer-Moore, but as I
say, in the vast majority of real cases it won't make any odds.
Depending on the length of the text being searched and the frequency, you
might want to consider a precompiled regex.
Anyway, you should perform some performance testing yourself. It really
depends on the circumstances.
Agreed. If you know you're going to have to search for the same string
lots of times in a performance-critical environment, it may be worth
using regular expressions. I would use Contains until I'd actually
proved it was a bottleneck though :)
Jon
.
- Follow-Ups:
- Re: strings vs regular expressions
- From: Henning Krause [MVP - Exchange]
- Re: strings vs regular expressions
- References:
- Re: strings vs regular expressions
- From: Michael Nemtsev
- Re: strings vs regular expressions
- From: Henning Krause [MVP - Exchange]
- Re: strings vs regular expressions
- Prev by Date: Re: Notification when a new column is added to a table
- Next by Date: Re: Net Framework 3 loads up 2 as well?
- Previous by thread: Re: strings vs regular expressions
- Next by thread: Re: strings vs regular expressions
- Index(es):