Please speed up my code
Afficher commentaires plus anciens
How can I speed up my code?
I know I can use parallel computing on while loop.
And I used gpuArray (quadro M6000) but runtime increased
I want to use CPU parallel computing or GPU
please modify my code which can run in the parallel computing tool
clear all
tic;
N = 1E5;
phi = linspace(0, 2 * pi, N);
V0 = 500;
Te = 3;
f =13.56E6;
w = f * 2 * pi;
tau = phi./w;
t = tau;
e=1.6E-19;
mi=40*1.6726E-27;
ni = 1E9*1E6;
eps0=8.8541878176E-12;
s0 = sqrt(eps0 * V0/ni/e/2);
% x = linspace(2 * s0,2 * s0, N);
x = 2 * s0 * ones(1,N);
sum_x = sum(x);
dt = 1E-10;
uB=sqrt(e*Te/mi);
% v = linspace(uB,uB,N);
v = -uB * ones(1, N);
le_i = [];
while sum_x > 0
s = s0 * (1 - sin(w * t));
ids1 = x >= s;
x_noE = x(ids1);
vx_no = v(ids1);
t_no = t(ids1);
ids2 = x < s;
x_E = x(ids2);
s_E = s(ids2);
t_E = t(ids2);
E = e * ni/eps0 * (x_E-s_E);
vx_E = v(ids2) + e*E/mi * dt;
v = [vx_no, vx_E];
x = [x_noE, x_E];
t = [t_no, t_E];
e_i = 0.5 * mi * v.^2;
x = x + v * dt;
t = t + dt;
idx = x<=0;
le_i = [le_i e_i(idx)./e];
idx = x>0;
x = x(idx);
t = t(idx);
if mod(1000*(N-length(x))/N,1) == 0
fprintf('Getting data... (%.1f %%)\n',100 * ((N-length(x)) / N));
end
sum_x = sum(x);
end
8 commentaires
Sargondjani
le 10 Mar 2021
Please format your code (and maybe make your code a bit denser, because it is quite long).
Anyway, it seems there is plenty of room for speed improvement. Just need to have a clearer picture of what your are doing.
darova
le 10 Mar 2021
You have Euler formula inside while loop. It can't be vectorized
Jan
le 10 Mar 2021
The first point to improve the speed is to omit clear all. This removes all loaded functions from the memory and Matlab has to relaod them from the disk. This has no advantages but it is a waste of time only.
Raymond Norris
le 10 Mar 2021
A couple of small points, most of these won't help with any signifcance. But first, I would suggest posting how long it takes to run and how long you'd like it to take.
- Only measure what can be improved. If you can't speed up the top, drop the call to tic to just what can (the while loop?)
- Preallocate le_i (size 1xN) and change le_i = [le_i ...
- Use a function
- Remove the fprintf. This saved 33% of the time. If you need it, descreas how often it prints.
At first glance, the while loop stops based on a condition and iteration I is dependent on iteration I-1. Don't see how you'll parallelize this.
inho seong
le 11 Mar 2021
Jan
le 11 Mar 2021
I do not see a way to parallelize your code, because each iteration is based on the results of the former one.
inho seong
le 12 Mar 2021
Jan
le 13 Mar 2021
Create your own copy of ttest, e.g. as "myttest.m" and omit all stuff, which is not needed by your call of this function.
Réponses (0)
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!