我想编译一个 Mathematica 模块,因为我追求速度。
testC = Compile[{{inputInt, _Integer}},
Module[{outputInt, bitShift = 5},
outputInt = BitShiftRight[inputInt, bitShift]
]
, CompilationTarget -> "C", RuntimeOptions -> "Speed"
, CompilationOptions -> {"ExpressionOptimization" -> True,
"InlineCompiledFunctions" -> True,
"InlineExternalDefinitions" -> True}
];
我的真实函数较长,但结构非常简单,使用列表,只包含以下函数:Total、Table、BitAnd、If。所有编译和运行时选项在我的实际功能中都很有用(也许),而不是这一行摘录。
我已经设定
SetSystemOptions["CompileOptions" -> "CompileReportExternal" -> True];
确保我能看到发生了什么,并且
编译打印[testC]
给出以下结果
1 argument
3 Integer registers
Underflow checking off
Overflow checking off
Integer overflow checking off
RuntimeAttributes -> {}
I0 = A1
I1 = 5
Result = I2
1 I2 = MainEvaluate[ Hold[BitShiftRight][ I0, I1]]
2 Return
正如该线程所预期/担心的那样https://mathematica.stackexchange.com/a/1101/1403 BitShiftRight 不可编译,并且对 MainEvaluate 的调用严重拖累了我的功能。我非常惊讶的是,这种非常低级的通用函数是不可编译的,而 BitAnd、BitNot、BitOr、BitXor 是!有人知道(快速)解决方法吗?我可以使用 MathLink 调用 C 语言函数,但我的目标是在 Manipulate[] 中使用此函数并将其部署为独立的 cdf 文件。而且我知道在这种情况下我不能使用 MathLink,对吗?顺便说一句,那里有一些易于处理的解决方法吗?