MATLAB Mex function isn't faster than regular function

后端 未结 2 1570
离开以前
离开以前 2021-01-26 17:14

I know that it wont always be the case, but aren\'t MEX functions supposed to increase efficiency in code, especially with calculations?

I have used the coder toolkit to

2条回答
  •  遇见更好的自我
    2021-01-26 17:53

    Here's how a call to expm(A) breaks down where A = rand(500,500);.

    enter image description here

    The time is split evenly between a matrix multiplication (F = F*F;) and a call to the child function PadeApproximantOfDegree. The matrix multiplication is built in, using very fast LAPACK functions in mkl.dll (MATLAB's linear algebra functions using Intel MKL).

    Here's where all the time is spent in PadeApproximantOfDegree:

    enter image description here

    That's not in a loop. All calls to built-in matrix math functions. If there were iterations, then I'd expect MATLAB to be a bit slower, but it's just a few lines with 1 call each taking up all the time. Only the matrix multiplication in the parent (F*F) is called more than once.

    In fact, I wouldn't be surprised if the MEX version were slower, if Coder were not able to use the optimized multi-threaded libraries that MATLAB has access to. Apparently Coder manages.

提交回复
热议问题