Filter löschen
Filter löschen

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

3 Ansichten (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.
function fit_param_mcode()
param = lsqcurvefit(@obj_fun)
% 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
% 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
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 Kommentar
Xingwang Yong
Xingwang Yong am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)


Mehr zu Get Started with Optimization Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by