Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strangely high use of CPU?

 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting lesson. Can you show an example of one of the listener loops that was eating CPU time?
 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for sharing.
 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the delay.

Thanks, it helped quite a lot both with the game, and my understanding
 
Jj Hill
Ranch Hand
Posts: 41
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you use excessive amounts of threads?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic