Under what circumstance, mex-version of lsqcurvefit run slower than m-code of lsqcurvefit?

2 views (last 30 days)
I converted a function to mex using Coder, which contains lsqcurvefit() to get some speedup. But this mex-version is slower than m-code.
function fit_param_mcode()
param = lsqcurvefit(@obj_fun)
end
% This take 1.6s and 1.5s was spent on objective evaluation
function fit_param_partial_mex()
param = lsqcurvefit(@obj_fun_mex) % only the obj_fun is mex
end
% 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().
function fit_param_full_mex()
... % both lsqcurvefit and obj_fun are mex
end
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?
  1 Comment
Xingwang Yong
Xingwang Yong on 30 Apr 2021
Does anyone know how to add "Tags" and "Products" after I post this question. I submited it by accident, without choosing related products. The "Edit" button does not help.

Sign in to comment.

Answers (0)

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!