

The problem can be solved by joining several Compile-able built-in functions together, but there are (perhaps several) "joints" where you face the performance-hit if using the top-level code, because it stays general and can not use specialized versions of these functions, and for a few other reasons. A very clear sign of it is when you have to do array indexing in a loop. So, some algorithms which are efficient in other functional languages may be inefficient in Mathematica). The problem is solved most efficiently with a procedural style, because for example an efficient algorithm for it is formulated procedurally and does not have a simple / efficient functional counterpart (note also that functional programming in Mathematica is peculiar in many respects, reflecting the fact that functional layer is a thin one on top of the rule-based engine. In my opinion, Compile as an efficiency-boosting device is effective in two kinds of situations (and their mixes): This is necessarily a subjective exposition, so treat it as such. I'll just throw in a few random thoughts in no particular order, but this will be a rather high-level view on things.

Please feel free to add any knowledge to this post. How to call for custom functions in the compiled code? This involves calls for external user functions, recursive calls (perhaps recursive calls passing arguments by reference), or directly injecting code into a held Compile.Īs I'm in the process of understanding Compile, I have a very basic understanding at the moment.Is it efficient/safe to leave the compiler to deal with type-conversions? Type-conversions: Compile silently converts Integer/ Real input to the appropriate type, and it also packs non packed arrays, but I'm not sure what the cost of it is.Which practice is better: partition a large computation into smaller compiled functions, or wrap the whole into one big function? What factors should be considered when making a decision? One such factor is for example that a large compiled function cannot easily return intermediate values (at least it is not trivial how to do this, see this post, and perhaps it also decreases performance).

x = x + c1 or x = c2, where the compiled function c2 contains the addition inside). Which operations should be left for the main evaluation to do? (e.g.boolean tensors are not supported (see this question, particularly Oleksandr's comment). There are structures which are not allowed by the Mathematica virtual machine, e.g.Which functions are allowed in Compile (see these lists: functions, distributions)?.

Some of these have already been answered (these don't need answers) but I included them for sake of clarity and completeness: What are the best practices of compiling functions? I understand that this is a vague question, but let me list some aspects that might trigger useful answers.
