468,290 Members | 2,097 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

How to make Regex.Replace faster?

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

Sep 14 '06 #1
4 2866
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
"Morgan Cheng" <mo************@gmail.comschreef in bericht
news:11********************@k70g2000cwa.googlegrou ps.com...
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

Sep 15 '06 #2
Morgan Cheng <mo************@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 15 '06 #3

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?
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
"Morgan Cheng" <mo************@gmail.comschreef in bericht
news:11********************@k70g2000cwa.googlegrou ps.com...
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
Sep 15 '06 #4
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" <mo************@gmail.comschreef in bericht
news:11**********************@b28g2000cwb.googlegr oups.com...

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?
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
"Morgan Cheng" <mo************@gmail.comschreef in bericht
news:11********************@k70g2000cwa.googlegrou ps.com...
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

Sep 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by bill tie | last post: by
5 posts views Thread by Greg Collins [InfoPath MVP] | last post: by
6 posts views Thread by Extremest | last post: by
3 posts views Thread by jwgoerlich | last post: by
6 posts views Thread by =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?= | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.