Using a function instead of inlining increases the time it takes to run
Show older comments
I use 64-bit Matlab R2014b.
I have a large matrix in which I iteratively change one element and I have another operation in which I sort a smaller matrix. I have two minimal working examples and I would like to know why one approach takes longer than the other. The first example uses a direct approach, whereas the second approach uses a function to perform the task.
Minimal working example 1
n_iter = 1e7;
A = zeros(n_iter,1);
B = sort(rand(1e3,1));
for i = 1:n_iter
A(i) = 1;
B = sort([ B(2:end); B(1) ]);
end
Minimal working example 2
function deallocating_large_matrices_function
n_iter = 1e7;
A = zeros(n_iter,1);
B = sort(rand(1e3,1));
for i = 1:n_iter
A(i) = 1;
B = sortB(B(2:end),B(1));
end
end
function B = sortB(B,new_entry)
B = sort([ B ; new_entry ]);
end
Results
Below a table showing the time it took to run both examples as a function of n_iter, using the profiler.
n_iter | time MWE 1 (sec) | time MWE 2 (sec)
--------------------------------------------
1e4 | 0.13 | 0.21
1e5 | 1.38 | 1.98
1e6 | 13.5 | 19.7
1e7 | 137 | 196
The time it takes to run both MWE 1 and MWE2 increases linearly. Running MWE 2 takes approximately 46% more time. What is the cause of this increase?
3 Comments
Adam
on 15 Dec 2014
I did read something concerning this not that long ago though I can't remember in what context.
A function has to be located in your path in order to be run so that will account for some overhead, although that is generally a first-time overhead and is faster on subsequent runs. It also shouldn't scale up with size.
I suspect it has something to do with a function having to create its own workspace which has an overhead, but again I'm not sure why this would scale up with data size other than that the data has to be copied by value to the function whereas inline this does not need to happen.
Adding this as a comment rather than an answer because it is not anything concrete, just musings based on my understanding of functions, but I don't have a deep understanding of exactly what goes on in Matlab with respect to function calls.
Jori
on 15 Dec 2014
Adam
on 15 Dec 2014
As far as I am aware it will recreate the function's workspace every call, although I don't know what kind of optimisations of the code take place at runtime - it may be clever enough to optimise that away.
I think new_entry in your above code will not take up any new memory, but B will, in your subfunction. This is because even though Matlab passes data by value rather than by reference, it is clever enough to not take a physical copy of data until or unless that data changes in your sub-function. At that point it will allocate memory and copy the data into it since it can't refer to the original any more.
If a variable is just input to a function, but remains unchanged in that function I am fairly sure no new memory is allocated to it and it just refers to the original in memory. I may be wrong there though and someone will hopefully correct me if I am.
Accepted Answer
More Answers (0)
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!