MATLAB Answers

0

Calculation speed

Asked by Jakob Sørensen on 27 Apr 2012
Latest activity Commented on by Andrew Jordan on 13 Feb 2018
Accepted Answer by Jan
I tried making a very simple algorithm for testing the computers speed, like this
tic;
for r = 1:2000;
for c = 1:2000;
A(r,c) = r^2+sqrt(c);
end
end
toc
And it's obviously on purpose that I didn't preallocate A, since I want to stress the processor a bit. Anyway, i ran it on three different computers with follow results:
University desktop, with Ubuntu and Matlab 2010b. Time ~18 sek
Home desktop, with Windows and Matlab 2011 (i think). Time ~7 sek
Home laptop, with Ubuntu and Matlab 2011 or 2012 (dont remember) Time ~4 sek
The thing that confuses me a bit, is that my Laptop is that much faster than my home desktop, since both processor, graphics card and ram should be superior on the home desktop. Can anyone help me solve this mystery?

  0 Comments

Sign in to comment.

5 Answers

Jan
Answer by Jan
on 27 Apr 2012
 Accepted Answer

Filling an array without pre-allocation does not stress the processor, but the memory. Therefore the processor is most of the time in sleep mode during your program runs. Matlab's collaboration with the memory manager of Window 7 is obviously better than with the manager of Ubuntu.
Matlab 2012a uses a new technique to reduce the drawbacks of a missing pre-allocation. It seems, like it allocates memory in larger chunks predictively, buit I do not know any details.
Therefore your measurements are not surprising. If you need a more detailed answer, post the exact description of the used operating systems and Matlab versions (version numbers and 32/64 bits). In addition post the amount of installed and available RAM as well as the type and speed of the RAM.

  0 Comments

Sign in to comment.


Daniel Shub
Answer by Daniel Shub
on 27 Apr 2012

Why not just use bench?
doc bench
Your lack of preallocation is probably distorting your results for your university desktop since automatic array growth gets a lot faster in R2011a
As for your two home machines, again the version may matter since MATLAB is always improving the JIT.

  1 Comment

Jan
on 27 Apr 2012
It is a disadvantage, that BENCH changes its problem sizes, such that the results are not comparable. But inspite of this, BENCH is still a more reliable measurement than populating an not-allocated array. +1

Sign in to comment.


Answer by Andreas Goser on 27 Apr 2012

Other contributing factors could be
  • 32/64 architecture of machine in combination of 32/64 MATLAB
  • BLAS routines, e.g. the MKL library for Intel processors

  0 Comments

Sign in to comment.


Answer by Jakob Sørensen on 27 Apr 2012

Aha, I'll use the bench function instead. Thanks to all of you, for being awesome! Except the fact that now I'll be playing with benchmarks rather that getting any work done... :-)

  2 Comments

Daniel Shub
on 27 Apr 2012
So you are using a recommendation I gave, but you accepted Jan's answer and didn't even give me an upvote :(
Jakob Sørensen on 27 Apr 2012
Oh, there's a vote function, didn't know, you got one now. Actually all of the answers where great, but I can't accept more than one.

Sign in to comment.


Answer by Jeremy Irons on 13 Feb 2018

clear all;
theta=0.002;
lambda1=0.0005;
lambda2=0.0008;
lambda3=0.0012;
I0=1;
x=linspace(-0.2,0.2);
I1=I0*(cos((pi./2)+(2.*pi.*x.*tan(theta))/lambda1).^2);
I2=I0*(cos((pi./2)+(2.*pi.*x.*tan(theta))/lambda2).^2);
I3=I0*(cos((pi./2)+(2.*pi.*x.*tan(theta))/lambda3).^2);
plot(x,I1,x,I2,x,I3)
legend('lambda=0.0005','lambda=0.0008','lambda=0.0012');

  3 Comments

Jeremy Irons on 13 Feb 2018
%2
clear all;
D=4000;
lambda=0.05;
I0=1;
d=5;
a=4;
tx=-80:1:80;
ty=tx;
[X,Y]=meshgrid(tx,ty);
I1=I0.*((sin((pi.*d.*Y)./(lambda.*D)))./((pi*d*Y)/(lambda.*D))).^2*I0.*((sin((pi.*d.*X)./(lambda.*D)))./((pi*d*X)/(lambda.*D))).^2;
hold on;
subplot(2,1,1)
surf(X,Y,I1)
xlabel('x')
ylabel('y');
subplot(2,1,2)
contour(X,Y,I1,200)
xlabel('x')
ylabel('y');
Jeremy Irons on 13 Feb 2018
%3
clear;
Na = 6.022*10^23;
kB = 1.38*10^-23;
u = 1.66*10^-27;
mH2 = 2*u;
mO2 = 32*u;
mN2 = 28*u;
v = 0:2000;
fvH2 = 4.*pi.*(mH2./(2.*pi.*kB.*300)).^(3./2).*v.^2.*exp(-(mH2.*v.^2)./(2.*kB.*300));
fvO2 = 4.*pi.*(mO2./(2.*pi.*kB.*300)).^(3./2).*v.^2.*exp(-(mO2.*v.^2)./(2.*kB.*300));
fvN2 = 4.*pi.*(mN2./(2.*pi.*kB.*300)).^(3./2).*v.^2.*exp(-(mN2.*v.^2)./(2.*kB.*300));
fvH2_2 = 4.*pi.*(mH2./(2.*pi.*kB.*70)).^(3./2).*v.^2.*exp(-(mH2.*v.^2)./(2.*kB.*70));
fvH2_3 = 4.*pi.*(mH2./(2.*pi.*kB.*500)).^(3./2).*v.^2.*exp(-(mH2.*v.^2)./(2.*kB.*500));
subplot(2,1,1);
hold on;
plot(v,fvH2)
plot(v,fvO2)
plot(v,fvN2)
xlabel('v [m/s]');
ylabel('f(v)');
legend('H2','O2','N2');
subplot(2,1,2);
hold on;
plot(v,fvH2)
plot(v,fvH2_2)
plot(v,fvH2_3)
xlabel('v [m/s]');
ylabel('f(v)');
legend('300K','70K','500K');
Andrew Jordan on 13 Feb 2018
1
clear all;
theta = 0.002;
lambda = [0.0005, 0.0008, 0.0012];
I0 = 1;
x = linspace(-0.2, 0.2);
I1 = I0*(cos((pi./2)+(2.*pi.*x.*tan(theta))/lambda(1)).^2);
I2 = I0*(cos((pi./2)+(2.*pi.*x.*tan(theta))/lambda(2)).^2);
I3 = I0*(cos((pi./2)+(2.*pi.*x.*tan(theta))/lambda(3)).^2);
plot(x, I1, x, I2, x, I3)
xlabel('X');
ylabel('I');
legend('0.0005','0.0008','0.0012');
title('Rozklad natezenia');
for j = length(lambda)
d(j) = lambda(j)/sin(theta);
end
figure
plot(lambda, d)
title('d(lambda)')
xlabel('Dlugosc swiatla lambda')
ylabel('Odleglosc d')
2
clear all;
D = 4000;
lambda = 0.05;
I0 = 1;
d = 5;
a = 4;
tx = -75:1:75;
ty = -75:1:75;
[X,Y] = meshgrid(tx,ty);
I = I0.*((sin((pi.*d.*Y)./(lambda.*D)))./((pi*d*Y)/(lambda.*D))).^2*I0.*((sin((pi.*d.*X)./(lambda.*D)))./((pi*d*X)/(lambda.*D))).^2;
hold on;
subplot(1,2,1)
surf(X,Y,I)
title('Rozklad natezenia')
xlabel('x')
ylabel('y');
zlabel('I')
subplot(1,2,2)
contour(X,Y,I,200)
title('Kontur natezenia')
xlabel('x')
ylabel('y');
zlabel('I')
hold off;
3
clear;
Na = 6.022*10^23;
kB = 1.38*10^-23;
u = 1.66*10^-27;
mH = 2*u;
mO = 32*u;
mN = 28*u;
v = 0:5000;
fH = 4.*pi.*(mH./(2.*pi.*kB.*300)).^(3./2).*v.^2.*exp(-(mH.*v.^2)./(2.*kB.*300));
fO = 4.*pi.*(mO./(2.*pi.*kB.*300)).^(3./2).*v.^2.*exp(-(mO.*v.^2)./(2.*kB.*300));
fN = 4.*pi.*(mN./(2.*pi.*kB.*300)).^(3./2).*v.^2.*exp(-(mN.*v.^2)./(2.*kB.*300));
fK70 = 4.*pi.*(mH./(2.*pi.*kB.*70)).^(3./2).*v.^2.*exp(-(mH.*v.^2)./(2.*kB.*78));
fK500 = 4.*pi.*(mH./(2.*pi.*kB.*500)).^(3./2).*v.^2.*exp(-(mH.*v.^2)./(2.*kB.*500));
subplot(2,1,1);
hold on;
plot(v,fH)
plot(v,fO)
plot(v,fN)
title('Rozklad predkosci czastek gazu Maxwella temperaturze 300K');
xlabel('v [m/s]');
ylabel('f(v)');
legend('H2','O2','N2');
subplot(2,1,2);
hold on;
plot(v,fK70)
plot(v,fH)
plot(v,fK500)
title('Rozklad predkosci czastek gazu Maxwella dla H2');
xlabel('v [m/s]');
ylabel('f(v)');
legend('Temperatura = 78K', 'Temepratura = 300K', 'Temperatura = 500K')

Sign in to comment.