views:

120

answers:

3

ok well i have a simple game that uses really high of memory and cpu. cpu goes over 44% and memory goes over 5000. here is my code

Code

how to fix this?


EDIT memory: 5000 bytes.
cpu: 44% on i5
the program get slower by the time it runs.

+1  A: 

That's too much code to examine thoroughly.

Some general tips though: Evaluate how much memory you expect it to use. Is memory use growing as it runs or does it stop growing at some point? If it continually grows you probably have a leak. There are packages out there that can help you track down where it's leaking, or make sure that you use RAII (for example shared_ptr) to manage your memory. If memory holds steady at a large number you may want to revisit your algorithm and see where the memory is being used. Are you allocating a lot of duplicate data?

As for CPU use, the only way to figure out where the time is going is to profile your application and see where the profiler says the CPU is being spent. Then you can approach that smaller section of code and determine how to improve it. The most likely improvements are finding polynomial (or worse) time algorithms and making them sub-polynomial time.

Mark B
well the memory won't stop growing so as the cpu
Ramiz Toma
@Ramiz use oprofile + valgrind
aaa
where can i get them
Ramiz Toma
+1 for profiling.
Brian
@Ramiz: Just google for C++ profiler and pick the one that is most appropriate (i.e. will work on the setup you are testing on).
Brian
@Ramiz http://elinux.org/Profilers
aaa
FWIW, I think applying a profiler to a program this small is overkill, unless you already have one set up and know how to use it. (But learning how to use a profiler is a good idea.)
Kristopher Johnson
In order to facilitate the good suggestion above, you should try to consolidate your code - stick the balls in an array/vector and get rid of ball1, ball2...You'll have a much better start on locating your performance issue.
+1  A: 

The best way to tackle something like this is to comment-out big chunks of code until you stop seeing runaway CPU/memory, and then gradually uncomment those chunks until you identify the problem.

After a quick scan of the code, I do wonder why you are starting eleven timers to update your game objects. It would be better to have a single timer that updates everything at once and then does a single Invalidate call.

Kristopher Johnson
You solved my cpu problem thanks
Ramiz Toma
A: 

Your high cpu load may come from your main loop.

I usually use something like

while(gameIsRunning) //Set this to false when the WM_QUIT message arrives
  {
  //handle all messages, if any
  while (PeekMessage(&msg, hwnd,  0, 0, PM_REMOVE)) 
    {
    if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
      {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
      }
    }

  //addition per loop code
  //...

  Sleep(1);
  }

Additionally, you should abandon timers and look into QueryPerformanceTimer and QueryPerformanceFrequency for timing and measuring.

Darcara