Re: Fastest way to search a string for the occurance of a word??
From: Niki Estner (niki.estner_at_cube.net)
Date: 08/11/04
- Next message: Mark Broadbent: "Re: open a database"
- Previous message: Özden Irmak: "Re: Get DefautlValue of a Property?"
- In reply to: Jon Skeet [C# MVP]: "Re: Fastest way to search a string for the occurance of a word??"
- Next in thread: Jon Skeet [C# MVP]: "Re: Fastest way to search a string for the occurance of a word??"
- Reply: Jon Skeet [C# MVP]: "Re: Fastest way to search a string for the occurance of a word??"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 11 Aug 2004 13:31:20 +0200
"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in
news:MPG.1b840d145a87447b98b121@msnews.microsoft.com...
> Niki Estner <niki.estner@cube.net> wrote:
> > > I usually find the exact opposite. Regular expressions are extremely
> > > powerful, but for simple string testing, I usually find a hard-coded
> > > test to be about an order of magnitude faster than using regular
> > > expressions.
> >
> > Really?
> >
> > I've build a little test sample, to find out what cases you mean, maybe
> > there's a mistake in it?
>
> Well, most of the kinds of tests I was talking about are doing more
> than just IndexOf - things like checking for a number being (probably)
> parseable, etc.
Maybe, but the OP's question was what's the "Fastest way to search a string
for the occurance of a word?".
> The test case you gave showed roughly the same results for both methods
> - sometimes faster for IndexOf, sometimes faster for Regex.
Really? in all the tests I did here, the Regex was by far superior.
Examples on my PC:
stringLength = 100; repeatCount = 100000;
Testing String.IndexOf: 00:00:00.3750000
Testing Regex.Match: 00:00:00.0625000
stringLength = 1000; repeatCount = 100000;
Testing String.IndexOf: 00:00:03.1406250
Testing Regex.Match: 00:00:00.5625000
stringLength = 10000; repeatCount = 100000;
Testing String.IndexOf: 00:00:02.3437500
Testing Regex.Match: 00:00:00.5312500
The Regex is about 5 times faster.
If the regex is compiled:
stringLength = 100; repeatCount = 100000;
Testing String.IndexOf: 00:00:00.3437500
Testing Regex.Match: 00:00:00.0625000
stringLength = 1000; repeatCount = 100000;
Testing String.IndexOf: 00:00:00.9375000
Testing Regex.Match: 00:00:00.1562500
stringLength = 10000; repeatCount = 100000;
Testing String.IndexOf: 00:00:10.6562500
Testing Regex.Match: 00:00:01.2031250
That's about 5-10 times faster.
> The way I
> see it, String.IndexOf *should* be faster, as it's a more blunt
> instrument - it doesn't need to work out anything clever about what the
> regex is doing, etc.
Wrong. RegEx's use the Boyer-Moore algorithm, which does take a little
time&space for initialization, but is magnitudes faster in matching. Also,
they can be compiled, in which case they whould have to be compared to a
matching routine specially built for finding one string.
> Using RegexOptions.Compiled did (on a single-test basis :) seem a bit
> faster, but that has implications in terms of memory use. (It's fine if
> you've got a static search string, but no good otherwise.)
Yes, that's why it's an option. You're not supposed to use it if you got new
regular expressions all the time.
> Of course,
> using regular expressions is likely to generate more garbage (if only
> the Match instance) than String.IndexOf (which really shouldn't be
> generating any garbage at all, I'd have thought).
If the regex doesn't contain any capturing paranthesis, it shouldn't
generate more than the Match object either (plus the initialization data
when the RegEx object is created).
> Personally I'd use IndexOf just for clarity's sake, to be honest.
I do too, if speed doesn't matter.
But again, that wasn't the OP's question.
Niki
- Next message: Mark Broadbent: "Re: open a database"
- Previous message: Özden Irmak: "Re: Get DefautlValue of a Property?"
- In reply to: Jon Skeet [C# MVP]: "Re: Fastest way to search a string for the occurance of a word??"
- Next in thread: Jon Skeet [C# MVP]: "Re: Fastest way to search a string for the occurance of a word??"
- Reply: Jon Skeet [C# MVP]: "Re: Fastest way to search a string for the occurance of a word??"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|