I am trying to speed up a code using Matlab coder, but the resulting mex code is actually slower than the m-file. The simplified version of the code that I am trying to speed up is attached below. I am wondering:
- Why the mex function is so much slower than the m-file function?
- Is there any way to improve performance of my code using Matlab coder?
Any hep would be much appreciated.
function v_d = choice_model_nop(q,v_exp,wealth,b_grid_choice,k_grid_choice,nz,nk,nb)
v_d = coder.nullcopy(zeros(nz,nk,nb));
parfor ii = 1:nz
q_ini_ii = reshape(q(ii,:,:),[],nk);
v_ini_exp_ii = reshape(v_exp(ii,:,:),[],nk);
choice = q_ini_ii.*b_grid_choice - k_grid_choice;
for jj = 1:nk
for kk = 1:nb
d = wealth(ii,jj,kk) + choice;
vec_d = d + v_ini_exp_ii.*(d>0);
v_d(ii,jj,kk) = max(vec_d,[],'all');
end
end
end
end
When I run the above code as an m-file I get:
>> timeit( @() choice_model(q,v_exp,wealth,b_grid_choice,k_grid_choice,nz,nk,nb))
ans =
0.2870
When I use matlab coder to convert the above code into a mex-file (without using JIT to use parallelization and with all speed options unchecked) I obtain
timeit( @() choice_model_mex(q,v_exp,wealth,b_grid_choice,k_grid_choice,nz,nk,nb))
ans =
0.3673
So the mex-function is actually slower. When I actually used squeeze instead of reshape the prerformance of the m-file was unaffected by mex-file was twice as low as using reshape...
In my testing q, v_exp,wealth are 61-by-61-by-61 arrays, b_grid_choice and k_grid_choice are 61-by-61 matrices, and nz,nk,nb are constants equal to 61.
So I am left wondering why the mex code is slower than the m-file. Is it because a large fraction of the time is spent using in-built matlab function? Is there a way to speed it up?
0 Comments
Sign in to comment.