Why a Matlab function is faster than the corresponding MEX?
Show older comments
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 Comments
Bruno Luong
on 20 Sep 2022
The JIT or engine execution also compile the matlab code. So MEX in some case is non longer superior.
People need to take out of their head the idea of for-loop == slow. This is no longer true since few latest MATLAB versions.
Walter Roberson
on 20 Sep 2022
Out of curiosity, what happens to the timings if you remove the if statement, converting to
for i=1:length(x)
d(i) = x(i,1)<=y(i,1) && x(i,2)<=y(i,2) && (x(i,1)<y(i,1) || x(i,2)<y(i,2));
end
Giulio Brandizzi
on 20 Sep 2022
Edited: Giulio Brandizzi
on 20 Sep 2022
Giulio Brandizzi
on 20 Sep 2022
Bruno Luong
on 20 Sep 2022
But Jan's VCookeK is manually designed and very carefully. This take a big amount of work and tuning.
You cannot compare the automatic translation such as coder.
In my experience, simple for-loop with arithmetic operations like yours, it is not worth to mex it. But if you have complicated algorithm and willing to spend time to look at availabe algoritms, etc.. then you can save time.
I would niot rely on coder for time is important, and it is probably first to evaluate what is te bottleneck of your code and focus on that part first.
Giulio Brandizzi
on 20 Sep 2022
Bruno Luong
on 20 Sep 2022
Edited: Bruno Luong
on 20 Sep 2022
Your for-loop is pretty straigh-forward, there is no much to be optimize about it unfortunately. You might try to create d with logical, and assign directly the boolean expression instead of branching with "if", migh be reverse the test so that less logical testing will be performed with your data. Again you have to mex manually so you can optimize it. Just forget coder to do the job for you.
Giulio Brandizzi
on 20 Sep 2022
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB Coder 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!