MATLAB Answers

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

5 views (last 30 days)
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.

  0 Comments

Sign in to comment.

Accepted Answer

per isakson
per isakson on 14 Aug 2020
Edited: per isakson on 14 Aug 2020
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 Comments

Jacob Lynch August
Jacob Lynch August on 14 Aug 2020
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.

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!