How can I make my code faster by vectorizing it?
显示 更早的评论
My code calculating equilibrium distribution works fine. But I was wondering that can I make my code below faster by vectorizing it?
function[tasaPainoJakauma_PS] = tPj_PS(lambda_R,lambda_A,mu_1,mu_2,n)
Q = zeros((n + 1).^2);
indeksi = @(x,y) y*(n + 1) + x + 1;
for x = 0:n
for y = 0:n
if x < n
Q(indeksi(x,y),indeksi(x+1,y)) = lambda_R;
end
if y < n
Q(indeksi(x,y),indeksi(x,y+1)) = lambda_A;
end
if x > 0
Q(indeksi(x,y),indeksi(x-1,y)) = mu_1 + x*mu_2/(x + y);
end
if y > 0
Q(indeksi(x,y),indeksi(x,y-1)) = y*mu_2/(x + y);
end
end
end
riviSummat = sum(Q,2);
for a = 1:(n + 1)^2
Q(a,a) = -riviSummat(a);
end
E = ones((n + 1)^2);
e = ones(1,(n + 1)^2);
tasaPainoJakauma_PS = e/(Q+E);
end
If so how?
4 个评论
Wick
2018-5-5
Almost certainly it can depending on how large 'n' gets. Can you provide some values for your inputs with which to test your function?
It seems to me that your two 'for' loops could be replaced by a direct assignment and the four 'if' statements could be replaced with a logical index.
The way you're indexing Q using indeksi is a bit non-traditional. I need some "real" numbers to make sure my vectorized code creates the same output array as yours.
Salama
2018-5-5
Wick
2018-5-5
It takes your computer 1000 seconds to run n=100 - or 100 runs takes 1000 seconds? If it's the former, I suspect you're running out of memory and swapping to the disk. That case took 7.4 seconds on my computer. It was using about 4 GB of RAM though.
Looking at your code, you potentially could speed up the 'for' loops and the 'if' statements. However, of my 7.4 seconds run, only 0.9 seconds was spent doing those steps. If you are actually running this 100 times then you could save, at best, 12% of your run time. It's the matrix solution that chews up the majority of the time and that's already vectorized.
Your matrix, 'Q' is mostly empty. Is there a way for you to solve the exact linear algebra system and code the solution directly? That might be faster - like solving a tridiagonal matrix doesn't require inverting the entire matrix. However, short of that, the run time isn't going to change substantially, I'm sorry.
Salama
2018-5-6
回答(1 个)
Here is a version with a sparse matrix - without a remarkable speedup:
function PS = tPj_PS(lambda_R,lambda_A,mu_1,mu_2,n)
m = (n + 1) .^ 2;
nQ = 1 + 6 * n + 5 * n^2;
Q = spalloc(m, m, nQ); % Not useful here!
% Q = zeros(m, m);
for x = 0:n
for y = 0:n
c = y * (n + 1) + x + 1;
if x < n
Q(c, y * (n + 1) + x + 2) = lambda_R;
end
if y < n
Q(c, (y+1) * (n + 1) + x + 1) = lambda_A;
end
if x > 0
Q(c, y * (n + 1) + x) = mu_1 + x*mu_2 / (x + y);
end
if y > 0
Q(c, (y-1) * (n + 1) + x + 1) = y*mu_2 / (x + y);
end
end
end
% Set the diagonal elements:
Q(1:(m+1):m*m) = -sum(Q, 2);
PS = ones(1, m) / (Q + 1);
end
The code for setting the diagonal was vectorized, but it was more important to replace Q+ones() by Q+1. Unfortunately most of the time is spent in ones/(Q+1), such that smart tricks to improve the loops will not help much. Even the sparse method does not help, because Q+1 is full. I admit, I posted it only, because I found out the formula of the number of non-zero elements...
The best speedup would be obtained by writing a solver for ones / (Q+1) which considers the known structure already - preferrably in a C-mex function.
类别
在 帮助中心 和 File Exchange 中查找有关 Link-Level Simulation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!