Compiled Functions in Mathematica

In summary, the conversation discusses the use of the command Compile in Mathematica for optimizing simulations. It is explained that this command defines a function that expects a numerical value for the variable and has a different set of rules when used inside a Compile[]. While it can significantly increase speed for certain problems, it is poorly documented and can be difficult to use effectively. Alternatives like MathLink to an external executable are also mentioned as potential options for optimizing speed.
  • #1
Lucid Dreamer
25
0
Hello,

I was looking at ways I could optimize a simulation I wrote in Mathematica and came across the command compile. From what I understand, it defines a function that expects a numerical value for the variable. But I don't understand how this would be any different from just ordinarily defining a function using " := "
 
Physics news on Phys.org
  • #2
When not inside a Compile[] something like + has to check

Is this a symbol, if so then do this
Is this a decimal approximation, if so then do this
Is this a matrix, if so then do this
...

All the checking adds a considerable overhead.

When inside a Compile[] the rules are very different. It invokes a much more limited set of sanity checks, if those fail it defaults back to the uncompiled code, otherwise it invokes something similar to a Java byte code machine which simulates a virtual CPU to do mostly numerical calculations.

For the right kinds of problems with a really well implemented algorithm inside Compile[] you can see 5-30x increase in speed. But Compile[] is at or near the top of the list for worst documented functions available. There are tiny little examples here and there in books and you can try to reverse engineer what the documentation should be to use it effectively. I am mostly serious with my offer to buy a cake for the first person to write a really really well done tutorial on how to write a Compile[] so that it will always do what I want fast and will work the first time. Thus far I have had zero nibbles on that.

You can also consider MathLink to an outside executable. If you can put almost everything you need to do into that executable and have minimal communication then that can provide you even greater speed.
 

1. What are compiled functions in Mathematica?

Compiled functions in Mathematica refer to a feature that allows users to convert their code into a more efficient form that can be executed faster. This is achieved by converting the code into a low-level language that can be directly executed by the computer's processor.

2. How do I compile a function in Mathematica?

To compile a function in Mathematica, you can use the Compile function, which takes in the code you want to compile and returns a compiled function. You can also specify the type of input and output data for the compiled function to optimize its performance.

3. What are the benefits of using compiled functions in Mathematica?

The main benefit of using compiled functions in Mathematica is the improved speed of execution. Since the code is converted into a lower-level language, it can be executed faster, making it ideal for handling large data sets or performing complex calculations. Compiled functions also save memory as they do not store intermediate results, resulting in more efficient memory usage.

4. Can all functions in Mathematica be compiled?

No, not all functions in Mathematica can be compiled. Some functions, such as those that rely on symbolic manipulation, cannot be compiled as they require the full Mathematica language to be executed. Additionally, functions that use external libraries or interact with the user cannot be compiled.

5. How can I check the speed difference between a compiled and non-compiled function in Mathematica?

You can use the Timing function in Mathematica to compare the execution time of a compiled and non-compiled function. Simply wrap the function or code you want to test with Timing[] and it will return the time it took to execute in seconds. You can then compare the times for the compiled and non-compiled versions to see the speed difference.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
397
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
245
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
888
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
21
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
925
Back
Top