# No Speedup Compiled Function in FindMinimum

The problem: I would like to minimize an objective function with respect to 12 variables. Minimization should be executed as fast as possible. Compiling the objective function increases the execution speed of the objective function but does not improve minimization speed.

1) Setup

• The Function is written in the following form:

FUNCTIONSYSTEMPART1 = Function[{time, c1, c2, rho, YY}, Evaluate[WORKINGSYSTEMPART1[[time]]]];

• In order to Speed up the process, I have compiled the objective function

COMPILEDSYSTEMPART1 = Compile[{{time, _Integer, 0}, {c1, _Real, 0}, {c2, _Real, 0}, {rho,_Real, 0}, {YY, _Real, 1}}, Evaluate[WORKINGSYSTEMPART1[[time]]]];

Note that the arguments time, c1 ,c2 and rho given as numerics. WORKINGSYSTEMPART1[[time]] represents the objective function. The only unknown variables are stored in YY which is a 12x1 Array.

2) Execution of Objective Function is much faster if the objective function is compiled:

In[400]:= AbsoluteTiming[
Do[FUNCTIONSYSTEMPART1[time, 1, 1, 0.55, ConstantArray[3, 12]], {j, 1, 100000}]]
Out[400]= {8.2444715, Null}

In[401]:= AbsoluteTiming[
Do[COMPILEDSYSTEMPART1[time, 1, 1, 0.55, ConstantArray[3, 12]], {j, 1, 100000}]]
Out[401]= {0.3740214, Null}


3) The Speedup is not preserved when the Minimization Function is called.

AbsoluteTiming[
Quiet[FindMinimum[FUNCTIONSYSTEMPART1[time, 1, 1, 0.55, Z], S, WorkingPrecision -> \[CapitalXi]0]]]
{0.3300188, ... }

AbsoluteTiming[
Quiet[FindMinimum[COMPILEDSYSTEMPART1[time, 1, 1, 0.55, Z], S, WorkingPrecision -> \[CapitalXi]0]]]
{0.3310190, ... }


Question: How is it possible that the speedup (attained by compiling the objective function) is lost?

-
This might also help: mathematica.stackexchange.com/questions/4700/… –  Ajasja Oct 22 '13 at 20:41
I don't see how Evaluate[WORKINGSYSTEMPART1[[time]]] can work as described. The variable time is not a actual integer at the time of compilation, but an argument of the function. –  Michael E2 Oct 23 '13 at 0:17

It seems that you use arbitrary precision arithmetics in FindMinimum. But you should realize that compiled function can work only with MachinePrecision (machine numbers, without arbitrary precision arithmetics). Apart of this, compiled function cannot be handled symbolically. It means that FindMinimum must use finite differences to estimate the derivatives instead of computing them symbolically. This will result in many more evaluations of the objective function.
@Breugem There is no option called MachinePrecision, please read the Documentation page for MachinePrecision to understand what it means. AFAIK Compile always works with MachinePrecision and it cannot be changed. If you specify WorkingPrecision for FindMinimum other than MachinePrecision you will still work with MachinePrecision if the objective function returns machine numbers. –  Alexey Popkov Oct 22 '13 at 19:07