Compiled Functions in Mathematica

  • Context: Mathematica 
  • Thread starter Thread starter Lucid Dreamer
  • Start date Start date
  • Tags Tags
    Functions Mathematica
Click For Summary
SUMMARY

The discussion focuses on optimizing simulations in Mathematica using the Compile function. It highlights that Compile significantly reduces overhead by limiting sanity checks compared to standard function definitions, resulting in speed increases of 5-30 times for well-implemented algorithms. The user expresses frustration over the lack of comprehensive documentation for Compile and suggests that a well-crafted tutorial could greatly benefit users. Additionally, the discussion mentions using MathLink to connect to external executables for further performance enhancements.

PREREQUISITES
  • Understanding of Mathematica programming language
  • Familiarity with the Compile function in Mathematica
  • Basic knowledge of algorithm optimization techniques
  • Experience with MathLink for external program integration
NEXT STEPS
  • Research advanced usage of Compile in Mathematica
  • Explore performance profiling tools in Mathematica
  • Learn about MathLink and its integration with external executables
  • Investigate best practices for algorithm implementation in Mathematica
USEFUL FOR

Mathematica users, computational scientists, and developers looking to optimize simulation performance and enhance their understanding of compiled functions.

Lucid Dreamer
Messages
25
Reaction score
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
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K