Improve the speed of nested for loops through vectorization or similar methods
Show older comments
My Matlab code has a function that is called 10^3 - 10^7 times. I'm curious if I can improve the speed of the function through vectorization or a similar method.
clc; clear all;
% Test data for function
u = rand(32,33);
Nx = 32;
Nz = 32;
Dz = rand(Nx+1,Nz+1);
u_z = zeros(Nx,Nz+1);
u_z_2 = zeros(Nx,Nz+1);
g = zeros(Nz+1,1);
% Method 1 - Original Implementation with double for loop
tic
for j=1:Nx
for ell=0:Nz
g(ell+1) = u(j,ell+1);
end
u_z(j,:) = (2.0)*Dz*g;
end
toc
% Method 2 - Remove one for loop
tic
for j=1:Nx
g=u(j,:)';
u_z_2(j,:) = (2.0)*Dz*g;
end
diff = norm(u_z - u_z_2,inf);
toc
Repeating these for loops 10,000 times gives
clc; clear all;
u = rand(32,33);
Nx = 32;
Nz = 32;
Dz = rand(Nx+1,Nz+1);
u_z = zeros(Nx,Nz+1);
u_z_2 = zeros(Nx,Nz+1);
g = zeros(Nz+1,1);
tic
for rep=1:10000
for j=1:Nx
for ell=0:Nz
g(ell+1) = u(j,ell+1);
end
u_z(j,:) = (2.0)*Dz*g;
end
end
toc
tic
for rep=1:10000
for j=1:Nx
g=u(j,:)';
u_z_2(j,:) = (2.0)*Dz*g;
end
end
toc
diff = norm(u_z - u_z_2,inf);
where the original implementation is slightly faster since the above code returns
Elapsed time is 0.771755 seconds.
Elapsed time is 1.079783 seconds.
Could the speed be improved through implementating vectorization or a similar method?
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB 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!