Consider this code:
ls = Table[{x, Sin[x]}, {x, 0., 10., 0.01}];
f = Interpolation[ls];
g = Compile[{{x, _Real}}, Evaluate@f[x]]
(*
CompiledFunction[{x},InterpolatingFunction[{{0.,10.}},<>][x],-CompiledCode-]
*)
Table[f[x], {x, 0., 10., 0.0001}]; // AbsoluteTiming
(*
{0.293382, Null}
*)
Table[g[x], {x, 0., 10., 0.0001}]; // AbsoluteTiming
(*
{0.436141, Null}
*)
Why the compiled version slower?
Compile
. See this question for a list of compilable functions. You can inspect yours by doingNeeds["CompiledFunctionTools`"];CompiledFunctionTools`CompilePrint@g
. You'll see a call to the main evaluator. Because it has to keep going back to the main evaluator for each point, it slows things down further. $\endgroup$ – rm -rf♦ Jul 9 '13 at 21:27Compile
knows ahead of time thatInterpolatingFunction
isn't compilable, code is generated for a call out of the VM, and since this is expected, no message is produced. $\endgroup$ – Oleksandr R. Jul 9 '13 at 21:44