Drastic processing time between calling a function with a square root operation in it and simply writing the function in the script itself
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
Basically I encountered a very large change in processing time because of where I put a sqrt function. Here is the code:
for i = 1:Rows
helloworld = i
for k = 1:Columns
for j = 1:OneExcelLength
% RO = DistanceFinder(DimLong(k),DimLat(i),OneLong(j),OneLat(j));
RO = sqrt((DimLong(k) - OneLong(j))^2 + (DimLat(i) - OneLat(j))^2);
if RO < WellRadius
ScoreMatrix(i,k) = ScoreMatrix(i,k) + LogBOPD(j);
end
end
end
end
Here, I do the square root function in the script and it runs really fast. But if instead I take out hte comment and call a function where I wrote this same sqrt line and send it paramenters and it sends back an ouput, it seems to take probably about 1000 times as long... thats not an exaguration. I was wondering why this would be?
0 commentaires
Réponses (1)
Sean de Wolski
le 17 Juin 2013
More than likely the matrix is preallocated in the script because it has run previously whereas the function creates a new workspace on each call.
Inside the function, do you have orange code analyzer warnings suggesting that the matrix changes size on every loop iteration?
2 commentaires
Sean de Wolski
le 19 Juin 2013
How long does it take? Are you timing multiple iterations of the same code?
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements 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!