0

我的代码有问题

在以下代码中:
GainDetailMatIMat9792*2448 矩阵的类型
ContrastGainBound4096xContrastGainLayerIint
平台:Android 4.4,NDK gcc 4.9

A:

Mat plus = ContrastGainLayerI * min(ContrastGainBound4096x, max(0, GainDetailMatI - 4096.0));

乙:

Mat t=max(0, GainDetailMatI - 4096.0);
Mat plus = ContrastGainLayerI * min(ContrastGainBound4096x, t);

A 比 B 多使用 13 毫秒。我通过在 Application.mk
中设置来关闭 gcc 优化APP_OPTIM := debug

有没有人知道原因?
我认为也许max(0, GainDetailMatI - 4096.0)返回类型MatExpr
t=max(0, GainDetailMatI - 4096.0);转换MatExprMat
也许这就是原因?
非常感谢!

4

1 回答 1

1

在示例 B 中,您首先将对象存储在 t 中,然后检索它以在代码的第二部分中使用。在示例 A 中,您跳过了存储和检索,从而使代码更高效。虽然这表明将所有代码转储到一行通常会提高效率,但请记住,可读性具有很大的价值。有关 Java 性能的更多信息可以在 wiki 上找到。https://en.wikipedia.org/wiki/Java_performance#Compressed_Oops

于 2016-04-29T09:35:37.020 回答