java - 提高java方法的效率

标签 java performance

我试图让该方法在 50 毫秒内完成,但我似乎不知道如何提高该方法的整体速度。我使用像素对象是因为在压缩数据时需要能够检查空值。

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);
}

这是 Pixel 类的构造函数

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;
}

最佳答案

不要使用 Pixel 类。您将 build 数十万个,甚至数百万个。

关于java - 提高java方法的效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20693481/

相关文章:

java - 如何在hibernate条件中使用mysql的 "use index()"子句?

java - 创建可扩展的 Java 库 (JAR)

javascript - $rootScope.$new 与 $scope.$new 的性能

sql - 哪个更好 : Parsing big data each time from the DB or caching the result?

PHP - 每次迭代查询单个值或在开始时获取所有值并从数组中检索?

java - 使用 Spring,如何获取具有特定实例的类的列表?

java - 如何通过 HttpPost 方法发送文本?

java - 生成一定长度的所有排列

performance - 将fpu切换为单精度

java - JAXBContext 初始化加速?