• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Game code with terrible performance - how to fix it?

 
Greenhorn
Posts: 2
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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):



Painting.Java:



Bullet.Java:

 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 2
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic