Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Application - Reduce CPU Usage

I have a multithreaded C# application, where the core logic sits in a Timer.Elapsed event handler. The event handler contains 2 for loops which executes a maximum of n * n = 5 * 5 = 25 times.

The application performs well on my PC. I ran VS 2010 Profiler against the application and CPU usage averages at 20%.

The company tester says that on his PC this jumps to between 50% and 100% on his PC. This is causing a performance issues for him.

  • Is there anything i can do to remedy this?
  • What does high CPU usage really mean?
  • Could this be impacting his PC?
  • Is it possible to tell an application to utilize only X amount of the CPU?

Any help would be appreciated.

like image 521
c0D3l0g1c Avatar asked Nov 08 '10 09:11

c0D3l0g1c


1 Answers

  • run the timer event less frequently
  • do the work on a worker thread (so the UI is at least responsive)
  • do less work in the timer (or do it more efficiently)
  • get more CPU

I'm guessing you really mean the third bullet, but we can't answer that without knowing what the code is doing; but (random suggestions without any context):

  • look at any collection access to see if there is a place for a dictionary, hash-set, or similar
  • check if you are doing vast amounts of IO (in particular to a DB) which could be reduced
  • check if you are going lots of thread-switches via Invoke (or the UI's equivalent)
like image 184
Marc Gravell Avatar answered Sep 16 '22 17:09

Marc Gravell