Under what circumstance, mex-version of lsqcurvefit run slower than m-code of lsqcurvefit？
1 Ansicht (letzte 30 Tage)
I converted a function to mex using Coder, which contains lsqcurvefit() to get some speedup. But this mex-version is slower than m-code.
param = lsqcurvefit(@obj_fun)
% This take 1.6s and 1.5s was spent on objective evaluation
param = lsqcurvefit(@obj_fun_mex) % only the obj_fun is mex
% This full mex version is converted from fit_param_mcode().
% This take 2.6s and 1.5 was spent on objective evaluation, meaning the
% rest 1.1s is spent on mex-version of lsqcurvefit().
... % both lsqcurvefit and obj_fun are mex
So, in my own case, the two versions take the same time to evaluate obj_fun, but self-time of m-code of lsqcurvefit() is much smaller.
I tried to reproduce the above result using other objective functions, such as exponential decay, sin(), cos(). But none of them reproduce the result, meaning the full-mex version is faster in these cases.
It seems this is related to my obj_fun, because my obj_fun does not have an analytical formula and it is based on simulation. Maybe mex-version of lsqcurvefit() take much time to do sanity checks and these checks are slow in mex.
Do you have any ideas?