• 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

javax.swing.timer taking up too much cpu (mac)

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys

I made this minigame for project (with a lot of stolen gifs lol, ill correct that later) and it works pretty well... except for its incredible cpu usage of 50%, even though the graphics are 8 bit and obviously dont take up a lot of graphics painting space... How the code works is pretty simple:



                       

The code and is attached (not the game because zip files are not allowed). I'm not sure what the problem is, is it the huge amount of arraylist for loops that might be lagging out the cpu? the duration of the timer has to be 5 otherwise the physics would be glitchy so I can't change that.

Also this is the mac version so sorry windows users :P I'm making the windows version later after I fix the copyright
 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ash Whin wrote:I'm not sure what the problem is, is it the huge amount of arraylist for loops that might be lagging out the cpu? the duration of the timer has to be 5 otherwise the physics would be glitchy so I can't change that.



That's 5 milliseconds per whatever it is you're doing repeatedly. Or 200 times per second if you want to look at it another way. I'd try doing the repetition a lot less frequently until you get a number where the performance and the physics work satisfactorily.
 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I edited the thread type and now the cpu usage is at 20%. Much better but not good enough for an 8 bit game :P How can i get it to around 10% and below?

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't do anything to the UI in any other thread. See Concurrency in Swing for more information. Also, you know that you're sleeping one millisecond each time there, right? That's basically the same as not sleeping at all.

Did you read Paul's comment on the number of repaints per second? 200 is much too high. Trying to get 60 is a lot more realistic. That would give you a delay of 16ms instead of 5.
 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried using some concurrency but it didn't do too much unfortunately, its stuck at 25% CPU with the following code SwingWorker and ActionListener combo:



I'm not getting this very well, can someone give me an explanation on what I'm doing wrong?
 
Ash Whin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . That's 5 milliseconds per whatever . . .

It is a long time since I tried any such timings on Swing, but when I did, I couldn't refresh a display in less than about 20ms.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is a difference between updating your game and displaying it. The updating can be done as many times per second as you like, or is feasable, while displaying could be done in another tempo, maybe every three to four updates. To get a decent balance between the two, you should do some measurements how long an update takes. Have you considered some hardware accelleration like JavaFX or OpenGL?
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thread.sleep(1); //Without this CPU usage is 150% What is going on here?




You should not be using a while true loop. This will cause the infinite loop which will hog the CPU.

You either need to either:

1) use a Timer to schedule some kind of animation or
2) have an end condition for the loop to stop.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are all sorts of confusing things about your code, apart from why are you writing while (true) ...?

Ash Whin wrote:

Those aren't doubles on lines 15‑16; they are ints. Why are you mixing doubles and floats? Why are you using floats at all? Just because line 20 compiles and runs, that doesn't make it is correctly typed. What does that bit about nanoTime mean in line 19? Why have you got a loop to reduce delta below 1? What is wrong with the % operator? Why are you trying to deal with sizes < 1?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic