为什么在这个方法中添加一个If语句会大大减慢它?

Fr3*_*dan 13 java performance android

我在回答另一个问题时遇到了这个问题.我试图诊断哪个代码更改对速度有更大的影响.我在for循环中使用了一个布尔标志来在使用辅助方法构建Color之间切换.

有趣的行为是,当我决定哪一个更快并删除了如果代码的速度放大了10倍.以前需要140ms,之后只需13ms.我应该只从循环中删除大约7个计算.为什么速度如此急剧增加?

慢代码:(运行时间为141毫秒helperMethods)*参见编辑2

public static void applyAlphaGetPixels(Bitmap b, Bitmap bAlpha, boolean helperMethods) {
    int w = b.getWidth();
    int h = b.getHeight();
    int[] colorPixels = new int[w*h];
    int[] alphaPixels = new int[w*h];
    b.getPixels(colorPixels, 0, w, 0, 0, w, h);
    bAlpha.getPixels(alphaPixels, 0, w, 0, 0, w, h);
    for(int j = 0; j < colorPixels.length;j++){
        if(helperMethods){
            colorPixels[j] = Color.argb(Color.alpha(alphaPixels[j]), Color.red(colorPixels[j]), Color.green(colorPixels[j]), Color.blue(colorPixels[j]));
        } else colorPixels[j] = alphaPixels[j] | (0x00FFFFFF & colorPixels[j]);
    }
    b.setPixels(colorPixels, 0, w, 0, 0, w, h);
}
Run Code Online (Sandbox Code Playgroud)

快速代码:(运行13ms)

public static void applyAlphaGetPixels(Bitmap b, Bitmap bAlpha) {
    int w = b.getWidth();
    int h = b.getHeight();
    int[] colorPixels = new int[w*h];
    int[] alphaPixels = new int[w*h];
    b.getPixels(colorPixels, 0, w, 0, 0, w, h);
    bAlpha.getPixels(alphaPixels, 0, w, 0, 0, w, h);
    for(int j = 0; j < colorPixels.length;j++){
        colorPixels[j] = alphaPixels[j] | (0x00FFFFFF & colorPixels[j]);
    }
    b.setPixels(colorPixels, 0, w, 0, 0, w, h);
}
Run Code Online (Sandbox Code Playgroud)

编辑:似乎问题不在于if在循环内部的事实.如果我提升if循环的外部.代码运行速度稍快但仍处于131ms的低速:

public static void applyAlphaGetPixels(Bitmap b, Bitmap bAlpha, boolean helperMethods) {
    int w = b.getWidth();
    int h = b.getHeight();
    int[] colorPixels = new int[w*h];
    int[] alphaPixels = new int[w*h];
    b.getPixels(colorPixels, 0, w, 0, 0, w, h);
    bAlpha.getPixels(alphaPixels, 0, w, 0, 0, w, h);
    if (helperMethods) {
        for (int j = 0; j < colorPixels.length;j++) {
            colorPixels[j] = Color.argb(Color.alpha(alphaPixels[j]),
                                        Color.red(colorPixels[j]),
                                        Color.green(colorPixels[j]),
                                        Color.blue(colorPixels[j]));
        }
    } else {
        for (int j = 0; j < colorPixels.length;j++) {
             colorPixels[j] = alphaPixels[j] | (0x00FFFFFF & colorPixels[j]);
        }
    }

    b.setPixels(colorPixels, 0, w, 0, 0, w, h);
}
Run Code Online (Sandbox Code Playgroud)

编辑2:我很蠢.真的很蠢.在调用栈中早先我用另一个布尔标志来使用该方法,并使用该使用另一种方法之间切换getPixel,而不是getPixels.我为所有具有helperMethod参数的调用设置了此标志错误.当我对该版本进行新的调用时,helperMethod我没有做到正确.性能的提升是因为getPixels不是if语句.

实际慢速代码:

public static void applyAlphaGetPixel(Bitmap b, Bitmap bAlpha, boolean helperMethods) {
    int w = b.getWidth();
    int h = b.getHeight();
    for(int y=0; y < h; ++y) {
        for(int x=0; x < w; ++x) {
            int pixel = b.getPixel(x,y);
            int finalPixel;
            if(helperMethods){
                finalPixel = Color.argb(Color.alpha(bAlpha.getPixel(x,y)), Color.red(pixel), Color.green(pixel), Color.blue(pixel));
            } else{
                finalPixel = bAlpha.getPixel(x,y) | (0x00FFFFFF & pixel);
            }
            b.setPixel(x,y,finalPixel);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:所有速度平均为100次.

aus*_*len 2

这可能是你的分析代码让你感到困惑。尝试隔离您想要分析的代码部分并仅测量该部分,避免 GCable 操作,例如在您的情况下创建位图。

如果我用以下命令调用你的测试代码

testing.loadDrawable(this, false, true, false)
Run Code Online (Sandbox Code Playgroud)

它运行缓慢。但如果我用

testing.loadDrawable(this, true, true, false)
Run Code Online (Sandbox Code Playgroud)

这是一个类似(甚至更糟)的数字。所以 useGetPixels 会带来很大的不同。我猜想将位图数据放入本地缓冲区并稍后设置结果。