我的代码有问题

在以下代码中:GainDetailMatI是具有9792 * 2448矩阵的Mat类型ContrastGainBound4096xContrastGainLayerIint平台:Android 4.4,NDK gcc 4.9

A:

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

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

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

有谁知道原因吗?
我认为max(0, GainDetailMatI - 4096.0)可能会返回MatExpr类型
并将t=max(0, GainDetailMatI - 4096.0);转换为MatExpr也许这是原因?
非常感谢!

最佳答案

在示例B中,您首先将对象存储在t中,并检索该对象以在代码的第二部分中使用。在示例A中,您将跳过存储和检索以使代码更高效。尽管这表明将所有代码都转储到一行通常会提高效率,但请记住,可读性具有很多值(value)。有关Java性能的更多信息,请参见Wiki。 https://en.wikipedia.org/wiki/Java_performance#Compressed_Oops

10-08 00:16