0
$\begingroup$

This question already has an answer here:

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?

$\endgroup$

marked as duplicate by Oleksandr R., Artes, Sjoerd C. de Vries, halirutan, m_goldberg Jul 10 '13 at 1:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    $\begingroup$ ... 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`"];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:27
  • $\begingroup$ @rm-rf I thought it would print out "proceeding with uncompiled evaluation" if evaluated with uncompiled version. It seems not the case, right? $\endgroup$ – xslittlegrass Jul 9 '13 at 21:32
  • $\begingroup$ 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. $\endgroup$ – rm -rf Jul 9 '13 at 21:35
  • 2
    $\begingroup$ 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. $\endgroup$ – Oleksandr R. Jul 9 '13 at 21:44
  • $\begingroup$ @OleksandrR. thanks for the explanation, but I still don't understand the difference, could you give or refer to some examples? $\endgroup$ – xslittlegrass Jul 9 '13 at 23:27

Browse other questions tagged or ask your own question.