aspose file tools*
The moose likes Game Development and the fly likes Strangely high use of CPU? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Strangely high use of CPU?" Watch "Strangely high use of CPU?" New topic
Author

Strangely high use of CPU?

Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Hi.

I wrote a game (picture below). It's a very small and simple game so far, you have the little rocketpod controlled by the player, and then the goal is to shoot down the incoming helicopters. Even though the game is so simple, when running, it brings the CPU load from 0% to about 38-39%. When I play a game like Call of Duty: Balck Ops, the CPU (an Intel i7 about 3.0 Ghz) is about 20-30% most of the time.

I have not been paying much attention to efficiency of the program as it's mostly just an attempt to make the game work.

So is this normal? Or is it most likely that it would become greatly less resource demanding with a litte optimization?


In-game picture

In advance, thanks
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

I would say it's not normal and you probably just haven't programmed it very efficiently!

Maybe you have some infinite looping in there (rather than acting on a timer), or you're trying to repaint the graphics too often? Have you tried profiliing it in an IDE, to see what's taking all the processing time?
Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Looks like loops are what are causing the trouble.

I thought that when i start a thread, if the run() method does not contain anything, i would stop working and stop being a listener (as the thread should die..? ). Therefore all my listener classes had a loop running, just to keep them alive . now all loops are removed, it still works, and it works damn good! and the CPU load peaks at 1% now.

Sweet! Thanks man!
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
Interesting lesson. Can you show an example of one of the listener loops that was eating CPU time?


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Sure

This is the listener class that detects if the player press the space key to fire.



The only thing I just have yet to understand is... The listener doesn't seem to be using a seperate stack given by the thread, so when the key is pressed, how does it happen in relation to the rest of code?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Read about the Event Dispatcher Thread (EDT) here. All listener methods in Swing/AWT run on the EDT, and the EDT stays alive as long as there is a visible Swing/AWT component (and in some other circumestances, such as a running Swing Timer).


luck, db
There are no new questions, but there may be new answers.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
Thanks for sharing.
Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Sorry for the delay.

Thanks, it helped quite a lot both with the game, and my understanding
Jj Hill
Ranch Hand

Joined: Jul 25, 2011
Posts: 41

Maybe you use excessive amounts of threads?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strangely high use of CPU?