473,785 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regex high cpu utilization

rh
hi all,
take the following 2 c# lines:
1) str = Regex.Replace(s tr, ".*AAA", "");
2) str = Regex.Replace(s tr, "^.*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 2011
"rh" <rh********@hot mail.com> wrote:
take the following 2 c# lines:
1) str = Regex.Replace(s tr, ".*AAA", "");
2) str = Regex.Replace(s tr, "^.*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
2843
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 culprits hasn't fingered any bad query in particular who's hogging CPU. One interesting anomaly is that there is a single type of query that averages 70 seconds of real time (with highs of 100-300 seconds) while expending zero seconds of CPU. The...
5
15213
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 partially understand since ASP.NET compiles the application on the first access. However, I also notice the high CPU usage by w3wp.exe after long periods of inactivity. For example, if I stop using the site for 30 minutes, and then come back to the...
1
7013
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 prefetchers returns: 2867326 1405102 3 Feb 24 - 1460:34 db2pfchr 0 3932384 1405102 0 Feb 24 - 42:48 db2pfchr 0
2
1495
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 on screen, a short time after CPU utilization moves up(according to Taskmgr), it stays at the new rate for a while, you scroll more it moves up more and stays a new higher value, after a few minutes of scrolling it will peg at 99%. Even if...
1
3163
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 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.
2
1844
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 with most of our apps in a custom app pool to recycle memory every 800MB.
4
4404
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 regxCarFile( L"(?=BAR)BAR{2}" ); bRet = boost::regex_search( wstrFilename, m, regxCarFile, boost::match_default ); if( true == bRet )
8
26780
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. I couldn't find out what to do on the server itself. I think it must be the application runnig on IIS. Could a MVP please confirm that this is mostly something that i caused by an application. So that I can give it back to our developers. Thanks
1
6630
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 memory utilization decrease. Why is this happening? PS: CPU Utilization is taken from Process\% Processor Time Memory Utilization is taken from Process\Working Set (I want to measure the physical memory it uses) Any help is appreciated....
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.