Wednesday, August 8, 2012

squeeze() is slow!


Test code to see how whether to use squeeze or use direct index addressing is faster

=========

clear all;


N = 50000;
tic;
for kk=1:N
x = rand(2,2,50);
y1 = x(1,1,:);
y2 = x(1,2,:);
y3 = x(2,1,:);
y4 = x(2,2,:);

z1 = y1(1,1,:).*conj(y1(1,1,:)) + y3(1,1,:).*conj(y3(1,1,:));
z2 = y2(1,1,:).*conj(y2(1,1,:)) + y4(1,1,:).*conj(y4(1,1,:));
end
toc;

tic;
for kk=1:N
x = rand(2,2,50);
y1 = squeeze(x(1,1,:));
y2 = squeeze(x(1,2,:));
y3 = squeeze(x(2,1,:));
y4 = squeeze(x(2,2,:));

z1 = y1.*conj(y1) + y3.*conj(y3);
z2 = y2.*conj(y2) + y4.*conj(y4);
end
toc;

=======

Execution Results :

Elapsed time is 2.188846 seconds.
Elapsed time is 11.824104 seconds.

Conclusion : do not use the squeeze() function in Matlab if you want speed!


No comments:

Post a Comment