# Why a Matlab function is faster than the corresponding MEX?

26 ビュー (過去 30 日間)
Giulio Brandizzi 2022 年 9 月 20 日
コメント済み: Bruno Luong 2022 年 9 月 23 日
Problem Statement:
I created a MEX function that is equivalent to the following Matlab function
function d = Mex_for_fun(x,y)
% Inputs: x and y are matrices of n (generic) rows and 2 columns
d=zeros(length(x),1);
for i=1:length(x)
if x(i,1)<=y(i,1) && x(i,2)<=y(i,2) && (x(i,1)<y(i,1) || x(i,2)<y(i,2))
d(i)=1;
else
d(i)=0;
end
end
end
The MEX function is simply obtained by using the Matlab coder in the app menu bar.
By comparing the run times of the two methods i used the following script:
%Test
Np=1000;
fitness=rand([Np,2])*1000;
all_perm=nchoosek(1:Np,2);
all_perm=[all_perm;[all_perm(:,2) all_perm(:,1)]];
x=(fitness(all_perm(:,1),:));
y=(fitness(all_perm(:,2),:));
%Matlab Function
tic
d_matlab_fun=Mex_for_fun(x,y);
toc
%Mex function
tic
d_mex=Mex_for_fun_mex(x,y);
toc
The results from the time on my pc are:
0.029305 seconds for the Mex function.
0.011935 seconds for the Matlab function.
Question:
Why is this the case? Shouldn't the Mex function be faster since it is compiled in C?
Thank you for the attention.
##### 8 件のコメント表示非表示 7 件の古いコメント
Giulio Brandizzi 2022 年 9 月 20 日
Ok thank you very much @Bruno Luong, you were very helpful.

サインインしてコメントする。

### 採用された回答

Apeksha Bagrecha 2022 年 9 月 23 日
It is important to remember that under the hood, a MEX file is simply a function that calls a C/C++ (or sometimes Fortran) subroutine. So when we are comparing the execution time of a MATLAB script to that of a MEX file, we are comparing the amount of time it takes the MATLAB script to be interpreted to the amount of time it takes the generated C/C++ code to execute.
Now it is reasonable to think that compiled C code should execute faster than M code is interpreted. However, it has been many years since MATLAB has been a true interpreted language. Today it is a just-in-time (JIT) compiled language with a large library of pre-compiled routines that are optimized for the target that MATLAB is running on. With MEX code generation, we are generating portable C code. Sometimes the MATLAB libraries are even multi-threaded while the generated code is not. Generally speaking, when an application mostly exercises pre-compiled binaries in MATLAB, or things that the JIT compiler handles well, the more realistic expectation is for MATLAB to be faster than the generated C code. The times when we see large speed-ups tend to correspond to those functions which are still implemented as complicated MATLAB functions in MATLAB. One might expect, therefore, to see speedups with QUADGK or QUAD2D, for example, but not with FFT.
With a simple script, we are really just comparing the C compiler to the MATLAB JIT compiler. For instance, the "mod" function is executing the exact same binary code in MATLAB as it is in a MEX file generated with MATLAB Coder. There just is not any opportunity for speedup here.
##### 2 件のコメント表示非表示 1 件の古いコメント
Bruno Luong 2022 年 9 月 23 日
Thank you @Apeksha Bagrecha for claering this out.
My observation on the simple loop withh expression on scalar (could be array element though) is that MATLAB become extremely efficient, and even my equivlent manual MEX cannot beat it. II then assume that MATLAB compile the for-loop code and not calling a library (since calling function is alway slow). My assumptionmight be wrong though.
In any case It is much harder to improve speed using MEX programing for sure.

サインインしてコメントする。

### カテゴリ

Find more on MATLAB Compiler in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by