0

我正在编写一个安卓renderscript程序。它有一个奇怪的性能问题。该程序有两个部分:

  1. 混合两个图像;
  2. 为混合图像添加特殊效果。

我测试了第1部分,它需要20ms;第 2 部分需要多花 10 毫秒。但是,当我从第 2 部分返回结果时,总执行时间变为 200 毫秒。

这是代码:

static uchar4 effect (uint32_t x, uint32_t y) {

    /* Part 1: Mix two images */
    const uchar4 *element1 = rsGetElementAt(gIn1, x, y);
    const uchar4 *element2 = rsGetElementAt(gIn2, x, y);
    float4 inColor1 = rsUnpackColor8888(*element1);
    float4 inColor2 = rsUnpackColor8888(*element2);

    float4 mixed = inColor1 * 0.5 + inColor2 * 0.5;

    /* Part 2: Special Effect */
    /* a lot computation here ... */
    /* a lot computation here ... */
    /* a lot computation here ... */
    float4 effect = ...;    /* computation result */

    float4 mixedEffect = mixed * 0.5 + effect * 0.5;

    /* Output result */
    return rsPackColorTo8888(mixed);        // fast
    // return rsPackColorTo8888(mixedEffect);   // very slow
}

void root(const uchar4 *v_in, uchar4 *v_out, const void *usrData,
    uint32_t x, uint32_t y) {
    *v_out = effect(x, y);
}

我做了三个测试:1)只有第1部分混合图像代码,并返回混合float4,20ms 2)第1部分和第2部分代码,并返回混合float4,30ms 3)第1部分和第2部分代码,并返回混合效果float4 , 200 毫秒

在第 2 次和第 3 次测试中,返回变量的变化导致整体性能变差。有谁知道为什么会这样?

4

1 回答 1

1

我认为有两件事正在发生。首先是当您不返回从混合效果派生的值时,编译器可以消除死代码,因为从未使用过结果。假设任何计算未使用值的代码实际上都不会运行。

第二个是你真的想把你的浮点常量写成 0.5f 而不是 0.5。0.5 指定了一个可能不是您想要的双精度值。

于 2013-06-17T00:44:11.103 回答