Java性能在MacBook和OSX上糟糕透顶,而在同一单元的Windows下则不然,为什么



我正在学习Java,并开始我的第一课/项目。在这个项目中,导师的像素在屏幕上移动,并在几秒钟内完成在屏幕上的移动。我的需要几分钟,每5秒只移动1个像素。我本以为MacBook Pro会更好地处理Java图形。为了确定这是我的代码还是单元,我在Windows中启动时从头开始重新构建了项目,性能明显提高。在那里,光标每秒移动1-2个像素;仍然不如教练,但比OSX处理得更好。我想知道这是正常的还是意料之中的?OSX只是在处理Java图形方面遇到了麻烦吗?如果有助于回答这个问题,我已经链接了我的代码,以及OSX中像素移动缓慢和帧速率糟糕的视频。Fraps显示我在Windows中的平均帧速率为650帧/秒,OSX端的代码输出显示它在40-60左右,这取决于我是否有其他视频进程。在视频中,它大约是45帧,但这是因为屏幕捕捉将其从平均60帧/秒减慢。

OSX中的帧速率示例:https://www.youtube.com/watch?v=PQ0-L4slgP4

屏幕类代码:http://pastebin.com/3ETKsY8r

游戏类代码:http://pastebin.com/NDreVB60

我在苹果端使用10.7.5以下的Eclipse Juno,在Bootcamp端使用Windows 7。MacBook拥有4GB内存和2.53 Ghz英特尔酷睿2双核。

我在MacBookPro上运行了您的代码示例,结果比您发布的视频中的要好得多。

现在,我不是Java 2D Graphics的超级专家,但有一点是,由于每次迭代都需要一遍又一遍地重新绘制整个画布,以便像素移动,因此渲染过程中涉及的逻辑应该很快。此外,由于像素是对角移动的,因此右侧较大的区域对您的示例没有用处,因此我建议将JFrame设置为正方形,这样可以减少重新绘制的区域。

最后,我对Screen类的代码进行了一些更改,这可以使您的操作更快。

package com.oblivion.rain.graphics;
public class Screen {
// Height was removed since there is no use for it
private int width;
public int[] pixels;
int time = 0;
int counter = 0;
// We need this in order to avoid the iteration in the clear method.
int previousPixel = 0;
public Screen(int width, int height) {
this.width = width;
pixels = new int[width * height]; // 50,400
}
public void clear() {
// In case the frame is a rectangle, the previousPixel
// could hold a value that is greater than the array size
// resulting in ArrayOutOfBoundsException
if (previousPixel < pixels.length) {
pixels[previousPixel] = 0;
}
}
public void render() {
counter++;
if (counter % 100 == 0) {
time++;
}
// Calculate the previousPixel index for use in the clear method
previousPixel = time + (time * width);

// Make sure we didn't exceed the array length, then set the 
// array data at the calculated index
if (previousPixel < pixels.length) {
pixels[previousPixel] = 0xff00ff;
}
}
}

相关内容

最新更新