Compiled version of interpolation function slower non-compiled version [duplicate]

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?

• ... because it is not compiled. You can't compile anything just by wrapping it in Compile. See this question for a list of compilable functions. You can inspect yours by doing Needs["CompiledFunctionTools"];CompiledFunctionToolsCompilePrint@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. – rm -rf Jul 9 '13 at 21:27
• @rm-rf I thought it would print out "proceeding with uncompiled evaluation" if evaluated with uncompiled version. It seems not the case, right? – xslittlegrass Jul 9 '13 at 21:32
• Hmm... I don't know exactly under what circumstances that message is produced, but I've always used the CompilePrint function to inspect the result. Maybe someone who knows more about this can comment. – rm -rf Jul 9 '13 at 21:35
• The message is only printed if a run-time error is encountered and the function has to be re-evaluated at the top level. Since Compile knows ahead of time that InterpolatingFunction isn't compilable, code is generated for a call out of the VM, and since this is expected, no message is produced. – Oleksandr R. Jul 9 '13 at 21:44
• @OleksandrR. thanks for the explanation, but I still don't understand the difference, could you give or refer to some examples? – xslittlegrass Jul 9 '13 at 23:27