How does MATLAB REALLY pass arguments to functions?

5 views (last 30 days)
Matvii Kistaiev
Matvii Kistaiev on 27 Apr 2022
Commented: Jan on 2 May 2022
I know that MATLAB might pass non-handle objects arguments "by reference" for optimization's sake. For example when argument isn't changed in function. But in practice it seems way more complicated to me.
Let's see the function for recursive quick sort:
function input = quick_sort(input, s, e)
%--RECURSION BREAK---
if (e - s) < 1
return;
end
%--PARTITION (This part isn't really related to the question)---
i = s;
j = e;
m = input(s + floor((e - s) / 2));
while (true)
while (input(i) < m)
i = i + 1;
end
while (input(j) > m)
j = j - 1;
end
if i < j
[input(i), input(j)] = deal(input(j), input(i));
i = i + 1;
j = j - 1;
else
break;
end
end
%--RECURSIVE CALLS---
input = quick_sort(input, s, i - 1);
input = quick_sort(input, j + 1, e);
end
As I understand, it "should" copy whole vector on each rucursive call. And it obviously "should" take additional time.
While this code does the same, but uses special wrapper handle-class for passing an array "by reference":
function input = quick_sort_h(input, s, e)
%--RECURSION BREAK---
if (e - s) < 1
return;
end
%--PARTITION---
% Same as in previous code snippet, but uses input.arr(...) instead of input(...)
%--RECURSIVE CALLS---
input = quick_sort_h(input, s, i - 1);
input = quick_sort_h(input, j + 1, e);
end
Wrapper class:
classdef largematrix < handle
properties
arr %Bruh
end
end
But when I measure time taken by these two algorithms, they show comparable results (handle one is actually even slower):
(There also a "subarray copying", it's when I copy only part of an array that i need to pass recursively)
So am I correct to assume that MATLAB somehow copies the array initially and then passes it by reference internally (or something like that)? Or is just handle class that slow?
I also haven't find any complete explanation how MATLAB behaves internally in such situations, so would also appriciate if someone explain it thoroughly.

Accepted Answer

Jan
Jan on 27 Apr 2022
By the way, at least in the online version, changing the line
[input(i), input(j)] = deal(input(j), input(i));
to
[input(i), input(j)] = swap(input(i), input(j));
% Yes here the same order! i,j <-- i,j
...
function [a,b] = swap(b, a) % This function has no body!
end
reduces the runtime by a factor 10 for sorting 1e5 random numbers.
  13 Comments
Jan
Jan on 2 May 2022
@Rik: I agree. Premature optimization is a common programming anti-pattern.
Matlab is very efficient for writing stable code and reducing the time for debugging. The processing speed has a lower priority. If I had written the tool for clinical decision making completely in C, it would run with perhaps the double speed, but the debugging and a migration to another compiler or graphics library would take half a year. In my case I can let physiotherapists with limited Matlab skills write their on small add-ons without the danger to let the computer carsh.This would be impossible in C.
So for my total purpose, Matlab is the best choice. And if a specific detail of the JIT really matters, it is useful to contact the corresponding programmer personally.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!