I have the following functions:
Block[{n, diag},
snake`diag[n_] = Ceiling[1/2 (-1 + Sqrt[1 + 8 n])];
snake`alongdiag[n_, diag_] = {0, diag + 1} + (n - diag (diag - 1)/2) {1, -1};
snake[n_] = snake`alongdiag[n, snake`diag[n]];]
(*A003986 on OEIS, http://oeis.org/A003986*)
A003986[n_] := BitOr @@ (snake[n] - {1, 1})
A003986c =
Compile[{{n, _Integer}}, A003986[n], CompilationTarget -> "C",
RuntimeAttributes -> {Listable}, RuntimeOptions -> "Speed"]
The functions seem very "compilable" in that they're just numerical computations. However, the compiled function performs as badly or worse:
Table[{x, A003986 /@ Range@(10^x); // Timing // First,
A003986c[Range@(10^x)]; // Timing // First}, {x, 5}]~
Prepend~{"Func", "ME", "Comp"} // TableForm
Fine - perhaps the function does "too little" and the overhead from running the C code is costing too much. But then altering my code to fix that doesn't help things:
A003986l =
Compile[{{n, _Integer}}, A003986 /@ Range[n],
CompilationTarget -> "C", RuntimeOptions -> "Speed"];
Table[{x, A003986 /@ Range@(10^x); // Timing // First,
A003986l[(10^x)]; // Timing // First}, {x, 5}]~
Prepend~{"Func", "ME", "Comp"} // TableForm
Both functions are approximately linear, so everything seems to be working as expected in an algorithmic regard, but this is very slow for a couple of formulas and BitOr
! What is going wrong?
CompiledFunctionTools`CompilePrint
onA003986c
-- I bet it shows aMainEvaluate
. $\endgroup$ – Michael E2 Jan 29 '14 at 17:49On[Compile::noinfo]
will cause a warning to be printed when you compileA003986c
. $\endgroup$ – Michael E2 Jan 29 '14 at 17:55Compile[ ... , Evaluate@A003986[n], ...]
, otherwise the function won't be inlined. $\endgroup$ – Szabolcs Jan 29 '14 at 18:31