Effacer les filtres
Effacer les filtres

logical statement accelerates or decelerates speed?

3 vues (au cours des 30 derniers jours)
Jiangmin zhang
Jiangmin zhang le 7 Déc 2012
i have the following code. i intended to check whether logical statement will affect the speed significantly.
it turns out to be quite strange. the first loop is twice faster than the second.
can anyone explain it?
my matlab is 2011b. my laptop is back to 2007 and my system is Ubuntu.
==========================
f=1;
tic
for s=1:100
if f==1
a=rand(1000,1000);
a=a*a;
end
end
toc
tic
for s=1:100
a=rand(1000,1000);
a=a*a;
end
toc

Réponses (3)

Azzi Abdelmalek
Azzi Abdelmalek le 7 Déc 2012
Modifié(e) : Azzi Abdelmalek le 7 Déc 2012
It's because, when the second for loop wa executed, your computer was more busy. I tried your code, the second for loop was faster 43 sec against 49 sec
  1 commentaire
Azzi Abdelmalek
Azzi Abdelmalek le 7 Déc 2012
If I have to test the effect of If ,I will choose a simple code
f=1;
t1=0;t2=0;
for s=1:1000000
tic;
if f==1
a=1;
end
t1=t1+toc;
tic;
a=1;
t2=t2+toc;
end
t1
t2

Connectez-vous pour commenter.


Muruganandham Subramanian
Muruganandham Subramanian le 7 Déc 2012
Because, when you are increasing the no. of logical expression,leads to increase the memory allocation (i.e both temporary and data lenghts) and it depends on data types also..

Fangjun Jiang
Fangjun Jiang le 7 Déc 2012
Modifié(e) : Fangjun Jiang le 7 Déc 2012
Use profile to find out how much time each statement takes.
I ran your code. It is strange that first loop is faster than the second.
Elapsed time is 6.062211 seconds.
Elapsed time is 6.134868 seconds.
However, if you change the loop to be 1000 but the size of a to be 100, the result is different. I think the impact of the f==1 statement is so minuscule that it is hard to evaluate in this context.
Elapsed time is 0.307561 seconds.
Elapsed time is 0.272184 seconds.

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by