Speed and Efficiency: When to Evaluate the Right-Hand-Side during a Function Definition

Let's set a baseline to check efficiency. Here we check timing to integrate something

"index_102.gif"

"index_103.gif"

We check the same thing again, because Mathematica may have spent some time loading algorithms to integrate

"index_104.gif"

"index_105.gif"

Here, we time how long it takes to create a function (with delayed assignment), but using Evaluate on the rhs.

"index_106.gif"

"index_107.gif"

The following is equivalent to the above (safer) definition---and will work so long as c is not assigned to an expression.

"index_108.gif"

"index_109.gif"

The following should take the *least* amount of time to perform, but as we shall see is not as efficient in the long run.

"index_110.gif"

"index_111.gif"

"index_112.gif"

Global`DelayedEvaluated

"index_113.gif"

Global`Immediate

"index_114.gif"

Global`FunctionDef

"index_115.gif"

The following should give a rapid result

"index_116.gif"

"index_117.gif"

"index_118.gif"

The following will not be rapid, because it has to do the symbolic integration before returning the result.

"index_119.gif"

"index_120.gif"


Created by Wolfram Mathematica 6.0  (23 October 2007) Valid XHTML 1.1!