c++ using too much cpu

不想你离开。 提交于 2019-12-01 12:41:19

My guess would be that you are constantly re-allocating and re-drawing everything instead simply moving the images around. I can see the application frequently having to re-allocate and re-draw from scratch having a bottleneck on the system.

You can optimize a little the paint function. For example the creation of the four bitmaps could be done in an initialization phase since it is always the same files you are loading. Also, in all the for loops, you can make one call to the size function of your vector, store it in a temporary variable and use that variable in the for loop for the condition ( as long as the size of the vectors doesn't change in the for loops).

It's very hard to say without knowing more about usage patterns of the thing. A good profiler will do wonders - I've had good luck on Windows with AQTime, although it's not free which may not suit you.

A few observations which may or may not be relevant:

  • You are drawing using GDI+. It is not fast and may well not scale to what you need. If I wanted rendering of complex scenes, I'd find something else.

  • I am suspicious about you calling InvalidateRect in your Paint function. Seems odd to me (doesn't painting validate the image?). Also, you are still repainting everything regardless, so what's the point?

  • You are using erase() on a vector to erase multiple (possibly many) random entries. This has quadratic performance and should be avoided for large vectors.

  • In one spot there's a pair of nested loops (loop over all 'shots' followed by loop over all 'enemies'); those look like they'll scale badly to large numbers of both.

But you really need to find a profiler; I'm just guessing here.

Please try with the profiler that comes with VS 2010 details of which are here

That may be your best bet on this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!