我试图让方法在小于50毫秒内完成,但我似乎无法弄清楚如何提高方法的整体速度。我为像素使用了一个对象,因为我需要在压缩数据时检查null。java方法提高效率
public static Frame getFrame(Dimension d, Robot r, Rectangle s, Resolution rs)
{
int w = d.width;
int h = d.height;
BufferedImage b = r.createScreenCapture(s);
Pixel[] pixels = new Pixel[w * h];
for(int i = 0; i < w; i++)
{
for(int j = 0; j < h; j++)
{
pixels[j * w + i] = new Pixel(b.getRGB(i, j));
}
}
return new Frame(rs, pixels, true);
}
下面是像素级
public Pixel(int c)
{
if((c & 0xFF) == 0xA || (c & 0xFF) == 0xD)
c++;
if((c & 0xFF00) == 0xA00 || (c & 0xFF00) == 0xD00)
c += 0x100;
if((c & 0xFF0000) == 0xA0000 || (c & 0xFF0000) == 0xD0000)
c += 0x10000;
if((c & 0xFF000000) == 0xA000000 || (c & 0xFF000000) == 0xD000000)
c += 0x1000000;
color = c;
}
构造而这里的Frame类
public Frame(Resolution res, Pixel[] pix, boolean ignoreCheck)
{
if(!ignoreCheck)
{
if(pix.length < res.getTotalPixels())
throw new NotEnoughPixelsException(res.getTotalPixels() - pix.length);
else if(pix.length > res.getTotalPixels())
throw new TooManyPixelsException(pix.length - res.getTotalPixels());
}
resolution = res;
pixels = pix;
}
这个问题似乎是脱离主题,因为它是关于代码审查 - http://codereview.stackexchange.com/ – lifetimes
根据我的经验,解决性能问题的最佳方法是获取一个分析器,并查看JVM真的在花时间。其他的一切都是猜测的工作,你猜测,做很多工作,然后发现你的猜测是错误的。啊。分析器是你的朋友。 –
我不同意这是脱离主题。有一个具体的问题“我如何得到这种改进的表现”,答案表明可以给出具体的回应。这不是一般的“这个代码如何改进”。我同意它正在推动一些界限。 :) –