How does MATLAB REALLY pass arguments to functions?

8 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Jan
Jan il 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 Commenti
Rik
Rik il 2 Mag 2022
I think we see the difference here between the rule and the exception. 'normal people' should not write for the JIT because they will probably make mistakes that will be hard to catch. Optimizing for JIT (or EE, or whatever the exact term) is a high risk high reward undertaking. For most things Matlab is fast enough.
There are legitimate reasons for diving into details. That is one of the ways Yair makes a living, and apparently it is also important for Jan's job. I will still tow the party line that you should not be optimizing for the JIT. You should write logical, predictable code structures. That way Mathworks engineers can optimize the next JIT for that style of writing.
There are always exceptions. The difficulty is in figuring out whether your situation is one of them.
Jan
Jan il 2 Mag 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Parallel Computing Fundamentals in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by