Info

This question is closed. Reopen it to edit or answer.

Achieve better performance when modifying state of a Matlab class.

1 view (last 30 days)
I have the following class definition
classdef State < handle
properties
R=0;
end
methods
function inc(C)
C.R = C.R + 1;
end
end
end
and I perform the following test
M=10000
tic
x = State();
for i=1:M
x.inc();
end
toc
tic
x = State();
for i=1:M
x.R = x.R + 1;
end
toc
The first run in about 0.04 and the other in about 0.006 so there is great difference in performance and possible room of improvement. My question is there a way to achieve better performance in this example assuming I want to encapsulate the behavior(R must be private)?
For a real world setting you can assume my class will have more state variables which will also be vectors (still private) and inc function will be more complex.

Answers (0)

Community Treasure Hunt

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

Start Hunting!