473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 3127
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
5
by: Greg Collins [InfoPath MVP] | last post by:
I couldn't find anything in my searches... I'm wondering if there's a Regex (with or without additional C# code) that can convert a either "lowerCamelCase" or "UpperCamelCase" into a proper "Title...
10
by: Extremest | last post by:
I know there are ways to make this a lot faster. Any newsreader does this in seconds. I don't know how they do it and I am very new to c#. If anyone knows a faster way please let me know. All...
6
by: Extremest | last post by:
I have a huge regex setup going on. If I don't do each one by itself instead of all in one it won't work for. Also would like to know if there is a faster way tried to use string.replace with all...
3
by: jwgoerlich | last post by:
Hello group, I am working on a query string class. The purpose is to parse name-value pairs from incoming text. Currently, I am using the Regex code below. I have two questions. First, the...
6
by: =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?= | last post by:
Hi, I need to replace double quotes inside the text from the database with " to correctly display my text. I was trying to use Regex to perform such a task: Regex.Replace(text, "", """) to...
5
by: V S Rawat | last post by:
I was trying to use back-to-back replace functions to convert a url: str1 = str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2 6","&"); It didn't replace all 4 types of...
4
by: seberino | last post by:
I'm looking over the docs for the re module and can't find how to "NOT" an entire regex. For example..... How make regex that means "contains regex#1 but NOT regex#2" ? Chris
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.