Nested Functions vs Object-Oriented Programming: k-D Tree example and relative performance

조회 수: 11 (최근 30일)
My colleagues tend to criticize my use of nested functions in MATLAB, likening that to the professional no-no of modifying global variables. To placate those peers, I'll recode those methods that use nested functions into a handle-based object-oriented methods. Now, they accept that the current properties are accessible to the object's methods.
However, I have a specific instance where the nested functions implementation outperforms the handle-based object-oriented programming style: k-D Trees.
Here is the benchmark code run from the command window.
pdat = randn(130001,3); % position data
tic;a_tree = kDTree(pdat); disp(toc) % 9.0822 seconds
tic;i_tree = ikdtree(pdat);disp(toc) % 1.0869 seconds
I'm seeking insights into what is causing these performance differences.

채택된 답변

per isakson
per isakson 2020년 8월 14일
편집: per isakson 2020년 8월 14일
On my R2018b,Win10 the diffence is even bigger
% 18.585, 0.79143
% 18.937, 0.75923
The profiling result of get_root_index shows
that assigning values to the properties, LeftIndices and RightIndices, takes nearly all the time.
It's a known fact that assigning values to properties is slow in Matlab. See Is MATLAB OOP slow or am I doing something wrong? and Numerical operations are slow on class properties versus in workspace.
The execution engine (/JIT) is The Mathworks' trade secret.
  댓글 수: 2
Jacob Lynch August
Jacob Lynch August 2020년 8월 14일
편집: Jacob Lynch August 2020년 8월 14일
That is surprising! I would not have expected the MATLAB JIT compiler to incur such penalties for OOP. My code used to be entirely functional (not in the rigorous FP sense, just that it only used functions), but that code quickly became overcooked spaghetti: broken and unmanageable.
The performance gains I reaped when migrating to class-centric MATLAB programming (at the suggestion of my more seasoned Software Engineering colleagues) seem to be more the resulf of implementing better functions rather than better data structures.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Olaf Constantin Schmidtmann
Olaf Constantin Schmidtmann 2022년 5월 3일
Even in R2022a, the performance gap between these two is quite huge on my machine (MACI64). This contradicted my results of Andrew Janke’s matlab-bench and I wondered why.
Without digging too deep into the functions themselves, I found that the OOP version (subclass of handle) uses property validation, which is expensive at this point. Hence, the comparison is a bit unfair. ;)
If you remove the validation (lines 11-14), like so:
properties (SetAccess = protected)
LeftIndices %(:, 1)
RightIndices %(:, 1)
end
… the tables turned on my machine; the OOP version was even a bit faster:
OOP (kDTree, original): Elapsed time is 5.630146 seconds.
OOP (kDTree, no validation of indices): Elapsed time is 0.579906 seconds.
Nested (ikdtree): Elapsed time is 0.641538 seconds.
You may add a dependent variable for subclasses to keep the validation. Also you might compare a custom validation in the nested version…
Btw, I do not believe, that OOP is a silver bullet, but there is clearly more to the decision than performance issues.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by