?
Solved

CPU Usage -  C# App struggling

Posted on 2006-04-21
17
Medium Priority
?
1,238 Views
Last Modified: 2008-01-09
Questions:  

i use this XP tweaking software to tweak my PC to no end this morning in order to run my C# App.   but bottom line is--> when i run my App with a moderate amount of data flowing thru it seems to run ok...  BUT any more than a moderate amount of data seems to run up the CPU usage to ~ 60% on average..  i have no XP "frills" running.   i have 1.5 gig memory in my PC..   i only have open slots for up to 2 gig...   i could buy another 1/2 gig ,  but not sure how much juice that will add.

>  Does anyone have ideas or recommendations on how to make my PC screaming fast?   Maybe gamers have ideas on how to maximize the efficiency of the CPU?

>  Will adding a bit more memory to get to 2 Gig help?

>  I use a high-speed cable modem Premium service to receive data for my App...  8 megabit (Mb) download    upstream = 512 kilobits (Kb) per second...      Would upgrading to a T-1 line, or business service help?  

Any thoughts appreciated.
0
Comment
Question by:lblinc
  • 9
  • 8
17 Comments
 
LVL 12

Expert Comment

by:topdog770
ID: 16508890
Help me make sure I understand what your app is doing.

What is the app trying to accomplish?  What technologies are you using?  Are there any external systems the app is interacting with?  Then, what types of data and in what quantities is the application working on or with?
0
 

Author Comment

by:lblinc
ID: 16511098
topdog ~ thx for reply.   it's a realtime application, lot's of realtime data..  event updates can occur by the millisecond.   yes, the App calls API functions referenced through dlls.   i run WinXP operating system with no "frills" running alongside...   even Norton's is shut-off for now.    and using VS C# .Net
0
 
LVL 12

Expert Comment

by:topdog770
ID: 16511811
Ok.  

So it appears that you are receiving data in real time via tcp/ip or some external ( via internet ) source, with data coming in every second, or faster, and that your application processes the incoming data.

You mention the CPU usage at 60% as being an indicator of negative performance, but this by itself may not be so much of an indication that your computer needs more enhancement.  What other indications of high workload are you seeing?  You mention trying to "tweak the PC to no end" this morning.  

I'd start by analyzing where the slow down is occuring.  For example, you mention possible solutions being faster internet connection, more RAM, etc, but you stated that the CPU usage is at ~60%. Taken at face value, neither the faster connection or more RAM will 'automatically' reduce the CPU usage.

The first place that I'd look is at the amount of processing your application is performing on each piece of incoming data.  Useful pieces of information would include the size and format of incoming data, how your application handles the processing of the incoming data ( threading, object instantiation, etc ), the type and number of functions and external api calls required per incoming unit of data, what your app does with this ( assumption on my ) processed data, etc.

A sample of your incoming data, the code that your application performs on it and the result would be useful and necessary components in providing more focused feedback.


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lblinc
ID: 16513487
topdog.   excellent reply, thanks.   i will take all this into consideration because.. like you mentioned, i'm just not sure that adding more memory will solve the high CPU usage issue.  

in fact, my code and how it processes events in the handlers seems very efficient (and possibly the only way to do it using this API) , and... maybe the API is limited...  or the wrapper API code functions  (around C++ code) is limited somewhat.     or, maybe there is an issue with the amount of data and how it is requested from server, for instance.. could it be faster if the data was multi-cast or broadcast vs. the current method?  not sure..    i currently connect to data servers and request data through API functions which trigger events with  handlers that process what to do with the events from there...   i need to find a way to maximize thru-put of all this data out to classes..  I send data from the event processors over to a data Queue to then be distributed from there to certain class variables.   So the process is initially from the events triggered -->  event handlers -->  process events --> queue events --> send event data out to classes --> manipulate the data in the classes.

anyway, your logic is very good, and i think your paragraph here is the first/best approach -->

>  The first place that I'd look is at the amount of processing your application is performing on each piece of incoming data.  Useful pieces of information would include the size and format of incoming data, how your application handles the processing of the incoming data ( threading, object instantiation, etc ), the type and number of functions and external api calls required per incoming unit of data, what your app does with this ( assumption on my ) processed data, etc.

i think the next approach possibly is to run the App onsite at the vendor (data provider) site with direct access to their network / T1-lines and see how it runs there.   then determine if there's a more effiecient way to process all this realtime data from there.   my concern is that the API is possibly the limitation...and if that's the case then i don't know exactly what my options are from there as they only offer one API to access the data from their servers.

0
 
LVL 12

Expert Comment

by:topdog770
ID: 16513598
Your next steps sound promising.

I think it's safe to assume that this: " So the process is initially from the events triggered -->  event handlers -->  process events --> queue events --> send event data out to classes --> manipulate the data in the classes" HIGHLIGHTS why the processor can become tied up in processing.

In your current model, do you have to initiate activity to the server for every 'set' of event data?  If so that's one possible room for improvement, as you mentioned.

How or what are you defining as the indicator of less then desirable performance?  Depending on the quantity and frequency of the events and the size of data associated with each event and the processing and 'parsing' of event data AND the consumption of the event data.... 60% cpu usage may not be too bad.

Another option that could yield some strong benefits for you, depending on overall goals and constraints of your project, would be looking at ( if not already in place ) more use of threading and the addition of a second processor.  If 'real time' handling, processing AND consumption of the event data is desired, your project may be able to benefit by quite a bit from the boost in real time processing.

Another tidbit..

I've heard rumors and seen circumstantial evidence indicating that there's a performance hit between managed and unmanaged code.

Here's a little more detail quantifying the delay.


< snippet from http://www.devx.com/Intel/Article/7025 >

Calling a DLL via the Platform Invoke service has an overhead of up to 30 native X86 instructions per call. Additional cycles are consumed by the need to marshal arguments between managed and unmanaged functions. The marshalling cost varies depending on whether the argument types share a common representation in the managed and unmanaged environments (such as int and Int32).

For example, passing a String as an argument would increase marshalling cost for each function call, as managed strings are Unicode encoded and unmanaged strings are ASCII encoded. This encoding incompatibility would require the marshalling code to execute an expensive translation between Unicode and ASCII for each call into the DLL. You should account for these costs when determining the granularity of API calls, as it may be desirable to call higher-level unmanaged functions that have a chunkier API than to make a larger number of calls to a lower-level chatty API.

<snippet from http://csharpcomputing.com/Tutorials/faq.htm >

How fast is a .NET application compared to C/C++?
Many C#/VB.NET applications will run 10-30% slower than their unmanaged counterparts. The main performance hit in managed applications comes from a need to load the .NET runtime and then compile the application on the fly before actually running it. Currently it is not uncommon to wait up to 30 seconds for an ASP.NET page to load on 1.6Ghz  512MB Dell with 500 Mhz Rambus memory. Additional performance penalty occurs when ActiveX and COM components are accessed from .NET. Due to the complex marshalling protocol, it may take up to 80 CPU cycles to execute a function from an unmanaged component. As developers learn .NET optimization tricks and JIT compiler optimization becomes more efficient, performance of .NET applications will become comparable to C++.


0
 

Author Comment

by:lblinc
ID: 16516371
topdog ~  thank you again for input.     can you explain this sentence on this topic further?    you mentioned...

>    In your current model, do you have to initiate activity to the server for every 'set' of event data?  If so that's one possible room for improvement, as you mentioned.

 the technical support of the API asked me...  "Do you have to use everything for everything?"   iow ~  he wanted to know if i needed to use, or subscribe, to all the various data to get done what i need in my App.   My answer is..  yes..  i need access to all this data in order to run all necessary parts of the App.   i was also wondering what are the different methods or processes for requesting event data?   i am currently just using a while() block that advances (MoveNext) the enumerator with statements inside the while() that subsribe/trigger events.   A SubscribeCallback(string callback type, int state)  is used in this block,  and the same method can be used to request other types of event data as well.   not sure if there's another way that is better.

also, you mentioned...

>  Another option that could yield some strong benefits for you, depending on overall goals and constraints of your project, would be looking at ( if not already in place ) more use of threading and the addition of a second processor.  If 'real time' handling, processing AND consumption of the event data is desired, your project may be able to benefit by quite a bit from the boost in real time processing.

i only know some "basics" of threading, and currently use some basic threading in my app..  you say i could benefit by learning more in this area?

also, i don't know much about dual processors.   do you have any advice on how i can learn more about dual-processing or maybe how to convert my machine into a dual-processor (if possible?).   I currently have Intel Pentium 4 CPU 2.8 Ghz with 1.5 Gig RAM
0
 

Author Comment

by:lblinc
ID: 16516711
topdog  ~   btw  -  in the task manager on the "Processes" tab..   if you then right-click on any process...  you can 'Set Priority' to different levels:

Realtime
High
Normal
Low

etc...

You happen to know if this could be useful for me when running my App?   Thanks.
0
 
LVL 12

Accepted Solution

by:
topdog770 earned 1000 total points
ID: 16525810
What I meant to ask was whether the supplying entity pushed data to your application or your app made a request each time it wanted more data.  Looking at your earlier comments, it appears that your application make 'register for events' and then the supplying entity sends the event data to you as the event(s) occur.

The producer / consumer thread pattern could offer some benefits, at the very least some ideas on how you could use threading to handle some of the challenges you are solving.

Here's a couple of other ideas
------------------------------------

< Changing the performance options in XP to background services >
http://support.microsoft.com/?kbid=308417

< a few more performance tips>

http://kadaitcha.cx/performance.html
==
The advantages of dual processors appear to be tailored made to your applications environment with the large amount of data processing, need for near real time handling, and where CPU utilization is the bottleneck.  

Having a good threading pattern in place would make the move to a dual processor system optimum.  Without the threading aspect, moving to a dual processor system wouldn't be as likely to show big improvements.

==
Here's a little more on threading.. the concept and benefits

http://msdn.microsoft.com/msdnmag/issues/05/06/HyperThreading/default.aspx

Specific code examples
http://www.codeproject.com/csharp/#Threads

Code and some good information
http://www.csharphelp.com/archives2/archive417.html

Here's a sample of a real time app with threading that may offer some ideas

http://www.c-sharpcorner.com/UploadFile/mmehta/RealTimeMessageLoggingP111192005061954AM/RealTimeMessageLoggingP1.aspx?ArticleID=eae2d42a-547b-4eb8-a916-ca049156ca19
0
 

Author Comment

by:lblinc
ID: 16529027
btw  - i truly appreciate your input.   very helpful.

your statement above regarding "how" i access the data leads me to think this may be the exact reason why the "slowness" occurs when running a lot of data thru the app.

below code section is basically what i do to start /  run the events for one area of data that i need..  then there are other sections of code where i do the same as the below code to start the events...

I basically use IEnumerator and while() statement to iterate through a collection or hashtable...   i'm sure you get the idea..

private bool StartEventLog1()
{
   while (IEnum.moveNext )
   {
        Event += new EventHandler(function);

        SubscribeCallback(string Callback type, int state);
    }
}

I wonder then..  how i can get this firm (the supplying entity) to push data to my application rather than me making requests on the events?   that would be much faster, no?  that is what i'm doing above, subscribing or requesting the events.. yes?     i'm using their API and it only shows this one method( or function ) for subscribing to all these events.   hmm..  what to do next...   i think you nailed it..  but unfortunately this could mean that there's not much more i can do considering the API that i'm working with.  

the bottom line,  i can keep trying to maximize effieciency in both in the code and/or with the hardware..  but in the end I may still be limited to the amount of data that i can request at one time...    i would much rather that the supplying firm push the data down my throat, then let me handle it from there...    instead of me calling on these events and getting them when triggered.  

   



0
 
LVL 12

Expert Comment

by:topdog770
ID: 16529210
Definitely!  If you can get them to provide a push approach, you'll remove a major source of delays right off the bat!  ( And help them create a much quicker client piece )
0
 

Author Comment

by:lblinc
ID: 16529944
topdog  -  if you haven't listed above already..  so that i know how to discuss with the supplier of the data..

 -  do you know how i can learn more about the methods for accessing data from servers as you mentioned?  to make a quicker client piece?
 -  to understand better the differences between pushing data out to client vs. the callback subscription method that i'm currently using?

thanks again for everything.
0
 
LVL 12

Expert Comment

by:topdog770
ID: 16543584
Let me see what I can find today...
0
 

Author Comment

by:lblinc
ID: 16547936

topdog  ..   i noticed today that the app still seems to continue running even when the UI becomes totally unresponsive..    clicking on the App's UI (or clicking on the App's menubar)  sometimes takes 10 to 20 seconds for it to come back into realtime...  or not for minutes on end.     i have buttons on the UI that I need to be responsive at all times so the UI's "non-responsiveness" is not good.       usually the App will run for a few minutes in a healthy state whether the app is active (clicked-on) or not, however..  after this time period it can becomes unresponsive or feel like it is operating slow...   it is difficult to tell if the data is still flowing thru the app correctly at this point.    

by chance do you know what this could mean?    
0
 
LVL 12

Expert Comment

by:topdog770
ID: 16548208
It sounds as though all of the applications 'work' is being done in one thread.. resulting in the gui delays because the app is off doing so other functionality and only 'occasionally' checking for gui events.


One common approach is to separate the GUI from the working thread(s).

Here's a couple of, IMHO, great articles on the threading & GUI basically sorted by
signifigance from top to bottom

http://www.programmersheaven.com/zone30/articles/article513.htm

http://www.developerfusion.co.uk/show/4595/

http://dotnetjunkies.com/WebLog/johnwood/archive/2005/08/31/132267.aspx

http://www.codeproject.com/csharp/winformsthreading.asp
http://www.codeproject.com/csharp/csharp_guithreads.asp

0
 

Author Comment

by:lblinc
ID: 16556855
hey ~  i cannot thank you enough for the advice.   i will look over these links..    
0
 
LVL 12

Expert Comment

by:topdog770
ID: 16562022
as you work through things.. keep us posted.. I'm interested to hear what you end up with
0
 

Author Comment

by:lblinc
ID: 16562935
thanks.  i definitely will..  my fear is that they (supplier of data) will tell me that i will need my own data server in order to run the App with the amount of data that i need to request to run it effectively.   we'll see.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question