• Post Reply Bookmark Topic Watch Topic
  • New Topic

Poor performance  RSS feed

 
Henrik �stman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I'm having som performance problems with a program im writing.
The Applet should draw sprites to the screen, but i get very different performance numbers in Windows, Linux and OS X.
I'm aiming for about 60FPS, in Linux i get 67FPS, in Windows XP and OS X 10.4 i get about 24-27FPS. Using the exact code and the same computer for Windows and linux. Does anyone have any id�a why the numbers differs so much?? Running JDK 1.5 on all three plattforms! And by the way is: "Graphics2D offscreenGraph = (Graphics2D)OffscreenImage.getGraphics();" realy neccessary every frame update? I know that you should use getGraphics() on your JFrame since we could loose that display memory when switching to other programs, but this is my "virtual screen" for dubbelbuffering do i realy have to get a Graphics instance every frame?
Mainloops are included below, and sorry for my bad english!!

public void run()
{
Thread currentThread = Thread.currentThread();
while(mMainThread == currentThread)
{
mainLoop();
}
mMainThread = null;
}

private void mainLoop()
{
long mNewGameTime = System.currentTimeMillis();
mGameTimeDelta = mNewGameTime - mOldGameTime;
mOldGameTime = mNewGameTime;

Graphics2D offscreenGraph = (Graphics2D)OffscreenImage.getGraphics();

clearScreen(offscreenGraph);
drawGround(offscreenGraph);

// Uppdaterar alla spelobjekt.
for (int index = 0; index < mGameObjects.size(); index++)
((Sprite)mGameObjects.get(index)).update(mGameTimeDelta);

// Ritar ut alla spelobjekten.
for (int index = 0; index < mGameObjects.size(); index++)
((Sprite)mGameObjects.get(index)).draw(offscreenGraph);

if (DEBUG)
{
offscreenGraph.setColor(Color.MAGENTA);
offscreenGraph.drawString(FPS.getString(), mAppletWidth >> 1, mAppletHeight);
}

getGraphics().drawImage(mOffscreenImage, 0, 0, null); // kopierar �ver den virtuella ritytan till den riktiga bildsk�rmen.
//this.dispose();

FPS.addFrame();

try { Thread.sleep(15); }
catch (InterruptedException exception)
{
System.err.println("Fel vid sleep() i mainLoop(): " + exception.getStackTrace());
}
}

private void clearScreen(Graphics graph)
{
graph.setColor(new Color(152, 156, 252)); graph.fillRect(0, 0, mAppletWidth, mAppletHeight);
}
 
Gideon Goodwin
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, I have seen applets running on Mac OSX JVM run much slower than on windows or linux. That is strange in your case that the windows performance is also bad, usually I haven't seen that.

In any case, your main loops look fairly straightforward. It seems that you are making a game, in which case it is likely that a major portion of your screen will have to be redrawn each frame. If this is the case, you will in fact have to redraw the entire buffer. In some applications, you might be able to get more performance by only redrawing the "dirty rectangles", but if you have a lot of full-screen motion, the logic required to track dirty rectangles will take longer than any draw time you might be saving.

Have you profiled this code to see exactly which parts are taking up your cycles? This would be my next recommendation.
 
Gideon Goodwin
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You asked whether it was necessary to obtain a fresh Graphics each frame...in general, it is recommended to do this. You need to time the call to getGraphics() to see whether it's even worth worrying about (I'd suspect that the time-cost for this call is minimal).

You might be able to gain some performance by skipping the background clear. Recode your drawGround() method so that you take care of the background in one pass. That will save you from redrawing quite a few pixels.

Also, you could use your timing information to change the amount of time you sleep. This isn't exactly a performance increase, instead, you're just wasting less time if needed. If your main loop calculated that it was taking more than 15 milliseconds per loop, you might just want to skip the sleep period all together.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!