Essentially what it says in the title. Mathematica can export its code to C. How much overhead does that inflict on the code, as compared to writing it from scratch in C?
A lot depends on how you write your code in Mathematica. In my experience, the rule of thumb is that the generated code will be efficient if the code inside
So, if you write your code in this style and hand it to
As noted by @acl, one thing worth doing is to set the
in which case you will get warnings for calling external functions etc.
A good tool to get a "high-level" but precise view on the generated code is the
One not very widely known technique of writing even large
In summary - if you want your code to be as fast as possible, think about "critical" places and write those in "low-level" style (loops, assignments, etc) - the more it will resemble C the more chances you have for a speed-up (for an example of a function written in such a style and being consequently very fast, see the
In addition to the answers given, you may tweak specific commands to give better performance. For example
Another thing you might want to try (never needed this myself) is to give platform specific compile optimization options that your CPU supports:
I think default optimization is -O2.
Furthermore, for example basic arithmetic operations are quite optimized and linking to the runtime lib should be quite fast.
One important point I forgot, the internal optimizer will find a good way to formulate your expressions
Also, with the symbolic power you can simplify expression you could never do by hand or pen an paper....
To see what can be compiled have a look at:
Find get warning about external symbols not included you can alternatively use:
I don't have an answer but this is a bit hard to format in a comment. If runtime speed is your goal, I'd suggest using Compile with settings
I'm not certain about the inlining, and there may be other options worth tweaking to get the best speed. Also I would imagine to some extent the speed will depend on the optimization capabilities of the C compiler.
But then, I tend to imagine lots of things.
If you use the setting
Then you can actually export the C code and look at, or use ExportString to print it directly in Mathematica:
That call to ExportString ends with a function looking something like this:
So you can see that with