File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes Game code with terrible performance - how to fix it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Game code with terrible performance - how to fix it?" Watch "Game code with terrible performance - how to fix it?" New topic

Game code with terrible performance - how to fix it?

Maciej Cyranowicz

Joined: Nov 06, 2012
Posts: 2

Like in topic. I created a simple game prototype, and don't know why is it running very slow and taking 30% (!) of computer CPU power. Can someone look at this code an explain me where am I making performance errors?

This prototype is showing controlable ship at the middle of screen and planet with moon orbiting it, you can also shoot with space key. It should run at 60 FPS, but real speed seems to be much slower, at 20, maybe 25 FPS.

I am posting the whole code, but I think problem is somewhere in main loop or painting.

Frame.Java (main):



Martin Vajsar

Joined: Aug 22, 2010
Posts: 3728

Welcome to the Ranch!

I believe that the problem is with the timer resolution, which depends on the OS (and perhaps HW and other factors). On Windows, the timer usually has a resolution of 1/18 second, which sort of corresponds to the 20-25 FPS you report. I didn't ever need to use a high resolution timer in Java, so I cannot help on that, but Google might give you some ideas.

The 30% CPU usage is another hint that the paint is not called often enough. If your CPU was not able to draw more than 25 FPS you actually achieve, you'd have 100% utilization. Still, if you manage to get a timer with 60 calls per seconds, the CPU utilization will probably be somewhere around 70% (30%/25*60), which really is a bit too much. You should therefore use a profiler (such as VisualVM, most IDEs have an integrated profiler too) to find out what takes most CPU.

A few more points:

1) Drawing polygons can be expensive. In our company we've done some tests in the past and we weren't able to match the speed of native Windows application when drawing polygons in Swing. However, I'm not sure we've done everything right, and we have some very specific application to start with.

2) I'd probably create all the polygons (ship, planet, moon) upfront and then just reused them for drawing, even if it didn't improve the performance at all. It might help a bit with the performance actually, but I don't think it will be even noticeable.
Maciej Cyranowicz

Joined: Nov 06, 2012
Posts: 2

Realizing second part of your answer gave me about 20% CPU and 250% RAM usage perfomance. This is still far too much, so I lowered native FPS to 45 and noticed another boost, now game needs only 20-26% CPU and 70 mb RAM, but... this is still far too much. Any other ideas?
subject: Game code with terrible performance - how to fix it?
It's not a secret anymore!