473,386 Members | 1,828 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,386 software developers and data experts.

regex high cpu utilization

rh
hi all,
take the following 2 c# lines:
1) str = Regex.Replace(str, ".*AAA", "");
2) str = Regex.Replace(str, "^.*AAA", "");

notice that the only difference is that the pattern in line 2 has a
starter
marker (^). if str is large and does not contain the pattern, line 1
takes much much longer to complete than line 2 and cpu goes nearly
full throttle.

i have reproduced this behavior in .net 1.1 and 2.0. but jscript does
not exhibit this.

is this expected behavior? both lines do the exact same operation,
expect for line 2 we're explicitly instruction regex to start at the
beginning.
i'm baffled.

thanks,
rh

May 18 '06 #1
1 3116
"rh" <rh********@hotmail.com> wrote:
take the following 2 c# lines:
1) str = Regex.Replace(str, ".*AAA", "");
2) str = Regex.Replace(str, "^.*AAA", "");

notice that the only difference is that the pattern in line 2 has a
starter
marker (^). if str is large and does not contain the pattern, line 1
takes much much longer to complete than line 2 and cpu goes nearly
full throttle.

i have reproduced this behavior in .net 1.1 and 2.0. but jscript does
not exhibit this.

is this expected behavior? both lines do the exact same operation,
expect for line 2 we're explicitly instruction regex to start at the
beginning.


This is one possible expected behaviour under the current
implementation, which does some kinds of optimizations (such as
compiling to a dynamic assembly in memory if you wish) but not others
(as you have seen). Here's the way each one works in a string of length
n, assuming no optimization of any kind:

1)
Start at first character,
match next character * n times
end of string found, so back-track 1
try to match A, then end of string found | backtrack to .*
backtrack 1, try to match AA, then end of string | backtrack to .*
backtrack 1, try to match AAA, if found then success
not found, so backtrack the .* matching
Start at second character,
match next character * n-1 times
end of string found, so back-track 1
try to match A, then end of string found
backtrack 1, try to match AA, then end of string
backtrack 1, try to match AA, then end of string
not found, so backtrack the .* matching
Start at third character, ...

As you can see, this will take time proportional to n*n, or quadratic
time.

2)
Start at first character,
match next character * n times
end of string found, so back-track 1
try to match A, then end of string found
backtrack 1, try to match AA, then end of string
backtrack 1, try to match AA, then end of string
not found, so *NO MATCH*

This one can exit early because the '^' forces the expression to only
match the start of the string. It takes time proportional to n, or
linear time.

Now, semantically, one can see that '.*AAA' is simply the start of the
string up to AAA, and the whole string could be found quickly by just
searching for AAA. Since the '*' operator in .NET Regex is greedy, the
'^' is implied.

I don't know if the JScript test you ran has the same semantics, but it
appears it certainly has a different implementation with some more
optimizations. To make it quicker, you can use '^' yourself.

(I think this is a .net framework issue, not a general issue or a C#
issue, so I suppose it should have been posted to that newsgroup only.)

-- Barry

--
http://barrkel.blogspot.com/
May 18 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Evan Smith | last post by:
Scenario: new functionality was recently deployed in a 3-tier business application. Since deployment, CPU use has shot up to very high levels. Using event monitors to try to track down the...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
5
by: mcollier | last post by:
I am experiencing high CPU utilization with my ASP.NET application. The w3wp.exe process uses 75%-80% CPU for about 10 seconds. I notice this when the application first starts up, which I can...
1
by: pike | last post by:
Hi DB2 8.1 FP 7a on AIX 5.2.0. Can anyone tell me how best to reduce high CPU (and subsequent high IO) associated with db2pfchr processes? NUMIO_SERVERS is set to the default 3. A grep on these...
2
by: Glenn Thomson | last post by:
Hello: Use C# 2003 .Net Frameworks 1.1 SP1, OleDbConnection I open an Access data using a class (dataset has many pictures in it) and scroll back an forth using the four arrow keys. Recs displayed...
1
by: rh | last post by:
hi all, take the following 2 c# lines: 1) str = Regex.Replace(str, ".*AAA", ""); 2) str = Regex.Replace(str, "^.*AAA", ""); notice that the only difference is that the pattern in line 2 has a...
2
by: dm3281 | last post by:
Hello, all. I have 4 web servers in a farm using network load balancing service. All the web applications (there are about 50+ per server) are using ASP.NET 1.1. I have two apppools setup...
4
by: DSmith1974 | last post by:
Are lookarounds supported in the boost regex lib? In my VS6 project using boost 1.32.0 I can declare a regex as.. <code_snippet> std::wstring wstrFilename = L"01_BAR08"; boost::wregex...
8
by: =?Utf-8?B?dGFuaQ==?= | last post by:
Hello there, one of my servers is running windows 2003 and IIS 6.0. We have big performance issues on that server and I found out that a high CPU usage iniciated by w3wp.exe causes the problem....
1
by: Kaheru | last post by:
memory utilization increase? This is because when i try to keep track of the CPU utilization and memory utilization of my FTP server process (ftpserver.exe), the CPU utilization increase, but the...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.