Drastic processing time between calling a function with a square root operation in it and simply writing the function in the script itself
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Sean de Wolski
on 17 Jun 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 Comments
Sean de Wolski
on 19 Jun 2013
How long does it take? Are you timing multiple iterations of the same code?
See Also
Categories
Find more on Matrix Indexing 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!