Speed up matlab program with .dll file

Hi all,
I have a Matlab code with 4 for loops, and 1000 iterations for each loop. This code takes time and I would like to speed it up. Since the code can not be transformed into a vectorized version, so I think about converting M-Files to stand-alone applications (.dll for example). But I'm not sure if this can really make the execution faster. So if you have some experience about this, please share it to me.
By the way, if you know a easy method to convert a M-Files to a .dll file, can you tell me. Thank you in advance for your help.
Best regards,
Tuan

2 Comments

Please follow Jan's suggestions about whether your MATLAB code will be faster if compiled into C code, but just wanted to give you a heads up that one suggested way of accelerating MATLAB code is to generate a MEX-function from it using MATLAB Coder.
This is very useful to me. Thank Kaustubha Govind.

Sign in to comment.

Answers (1)

Jan
Jan on 13 Nov 2012
Without seeing the code, we cannot guess, if a compiled version will be faster. Usually a compilation does not lead to substantially accelerations, while an optimized code or a manual creation of a Mex file will.
Please post the relevant parts of the code with typical input values. If we can run it, I assume we find some improvements.

3 Comments

Tuan
Tuan on 13 Nov 2012
Edited: Tuan on 13 Nov 2012
Thank you so much for your prompt answer. Please find in the following my entire code for a quit simple version with 3 for loops (see StaLaw3DFuncDelTRpRuStra). FF1, FF2, FF3, FF13, FF12, FF23 can be transformed in to matrix version, I can do it even if there is something wrong for the matrix version (the results from matrix version and from for loop version are quite different). But the main point that I'm worry about is the second "3 for loops" to calculate Q. Hope to receive your help. Many thanks!
To run the code, you execute the main code
% Main code
tic
clear,
a1=2;a2=1;a3=3;b1=1;b2=1;b3=1;L1=10;L2=10;L3=10;Zinf1=30;Zinf2=30;
Zinf3=30;DelT=1;Ru=0.9;Rp=0.5;h=0.5;n=10;
z1p=HitDeg(a1,b1,L1,DelT,Rp); z1u=HitDeg(a1,b1,L1,DelT,Ru);
z2p=HitDeg(a2,b2,L2,DelT,Rp); z2u=HitDeg(a2,b2,L2,DelT,Ru);
z3p=HitDeg(a3,b3,L3,DelT,Rp); z3u=HitDeg(a3,b3,L3,DelT,Ru);
[X1,p1]=XEval([0 z1u z1p L1 Zinf1],h);
[X2,p2]=XEval([0 z2u z2p L2 Zinf2],h);
[X3,p3]=XEval([0 z3u z3p L3 Zinf3],h);
N1=length(X1); N2=length(X2); N3=length(X3);
F1=TransDens(X1,a1,b1,DelT,N1);
F2=TransDens(X2,a2,b2,DelT,N2);
F3=TransDens(X3,a3,b3,DelT,N3);
q123=zeros(1,n);
Q=[];
for i=1:n
[Q,q12,q13,q23,q123(i)]=StaLaw3DFuncDelTRpRuStra(X1,X2,X3,p1,p2,p3,F1,F2,F3,Q,N1,N2,N3);
end
figure, mesh(X1,X2,q12')
figure, mesh(X1,X3,q13')
figure, mesh(X2,X3,q23')
figure, plot(2:n,q123(2:n),'-o')
toc
% Calling function 1
function [Q,q12,q13,q23,q123]=StaLaw3DFuncDelTRpRuStra(X1,X2,X3,p1,p2,p3,F1,F2,F3,P i,N1,N2,N3)
% Given FF1+FF2+FF3+FF12+FF13+FF23+FF123;
Q=FF1+FF2+FF3+FF12+FF13+FF23+FF123;
p3_13=p3(1):p3(3); % indice vector for X3
p2_13=p2(1):p2(3); % indice vector for X2
p1_13=p1(1):p1(3); % indice vector for X1
for i1=1:N1
P1=repmat(F1(i1,p1_13)',[1 p2(3)-p2(1)+1 p3(3)-p3(1)+1]);
for i2=1:N2
P2=repmat(F2(i2,p2_13)',1,p3(3)-p3(1)+1);
for i3=1:N3
P3=F3(i3,p3_13); Q(i1,i2,i3)=Q(i1,i2,i3)+trapz(X3(p3_13),trapz(X2(p2_13),squeeze(trapz(X1(p1_13),Q(p1_13,p2_13,p3_13).*P1)).*P2).*P3);
end
end
end
Q=Q/trapz(X3,trapz(X2,squeeze(trapz(X1,Q))));
Where do I find the "second 3 for loops to calculate Q"? This is quite a big piece of code and without helpful comments it is impossible to keep the overview. Some of the lines are monsters and splitting them to smaller parts as well as using temporary variables for repeatedly calculated values would increase the code but most of all improve the readability.
Dear Jan Simon,
Sorry about my fuzzy code. I have reedited the code as your suggestion by preserving the main part that I'm worry about (see above section). The "3 for loops " is in the StaLaw3DFuncDelTRpRuStra function. A new value of Q is reused as input of trapz(), so a vectorized version of for loop is impossible. So, any way to speed up the program? Thank you!
Tuan

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 13 Nov 2012

Community Treasure Hunt

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

Start Hunting!