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?
Evaluate[WORKINGSYSTEMPART1[[time]]]
can work as described. The variabletime
is not a actual integer at the time of compilation, but an argument of the function. $\endgroup$