Hello Henning Krause [MVP - Exchange],
H> Anyway, you should perform some performance testing yourself. It
H> really depends on the circumstances.
That's the point, coz we dont know what the OP is looking for
>> A> hi,
>> A> I need to comapare or check for substrings in a given string.
>> A> which would give better performance - string related comapare
>> A> functions or
>> A> regualr expressions....
On Apr 11, 9:48 am, "Henning Krause [MVP - Exchange]"
<newsgroups_rem...@this.infinitec.de> 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
Henning Krause [MVP - Exchange] - 11 Apr 2007 14:48 GMT
Hi,
> 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 :)
"The First Rule of Program Optimization: Don't do it. The Second Rule of
Program Optimization (for experts only!): Don't do it yet." - Michael A.
Jackson