How to make Regex.Replace faster?

M

Morgan Cheng

In my case, I have to remove any line containing "0.000000" from input
string.
In below case, it takes about 100 ms for 2k size input string.
Regex.Replace(inputString, ".*0\\.000000.*\n", "");
I want to optimize it, so i make a static member instance instead of
using static func of Regex;
static Regex filter= new Regex(".*0\\.000000.*\n",
RegexOptions.Compiled);
And use the static instance to replace the string.
filter.Replace(inputString, "");
Now, it takes about 200 ms. The platform is .net 1.1.


I don't understand why the non-static member function is slower thant
static one. The static function has to compile the regular expression
each time, right?

Is there any better solution to remove substring of specific pattern?
Perhaps I have to write my code to do it.

Thanks,
-Morgan
 
C

Cor Ligthert [MVP]

Morgan,

To make regex faster try to avoid it by using a replace or an other method.

Regex is approx. 100 times slower in simple replacements than by instance
"replace".
(This will be gained again in complex situation where you would have to do a
lot of replacements of course)

I hope this helps,

Cor
 
J

Jon Skeet [C# MVP]

Morgan Cheng said:
In my case, I have to remove any line containing "0.000000" from input
string.
In below case, it takes about 100 ms for 2k size input string.
Regex.Replace(inputString, ".*0\\.000000.*\n", "");
I want to optimize it, so i make a static member instance instead of
using static func of Regex;
static Regex filter= new Regex(".*0\\.000000.*\n",
RegexOptions.Compiled);
And use the static instance to replace the string.
filter.Replace(inputString, "");
Now, it takes about 200 ms. The platform is .net 1.1.


I don't understand why the non-static member function is slower thant
static one. The static function has to compile the regular expression
each time, right?

Is there any better solution to remove substring of specific pattern?
Perhaps I have to write my code to do it.

I would try using a StringReader, read the data line by line, and
append any line which doesn't contain 0.000000 - simple string
operations. If you could provide a test program you've used to gather
your performance data, we could experiment a bit.
 
M

Morgan Cheng

Cor Ligthert [MVP] 写é“:
Morgan,

To make regex faster try to avoid it by using a replace or an other method.
Do you mean it is not recommeded to trim string with Regex?
So, the best way for is to code manually handling the string?
 
C

Cor Ligthert [MVP]

Morgan,
Do you mean it is not recommeded to trim string with Regex?
So, the best way for is to code manually handling the string?

If you can avoid Regex try that, but not in the case that you have to done a
lot of things for it, or make your code not readable for others anymore.
Than use Regex.

(And than you probably on the place that it performs in at least the same).

Just my opinion.

Cor


"Morgan Cheng" <[email protected]> schreef in bericht

Cor Ligthert [MVP] ??:
Morgan,

To make regex faster try to avoid it by using a replace or an other
method.
Do you mean it is not recommeded to trim string with Regex?
So, the best way for is to code manually handling the string?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top