Non-linear scaling for linear increase in complexity

10 views (last 30 days)
I have a class which defines an array and then loops over this array updating each element in turn. If I change the length of the array linearly the runtime increases non-linearly. Why? I must be missing something silly.
classdef C
properties
N uint32 {mustBeInteger, mustBePositive}
x (1, :) {mustBeFloat, mustBeNonnegative} = []
end
methods
function c = C(N)
arguments
N uint32 {mustBeInteger, mustBePositive}
end
c.N = N;
c.x = zeros(1, c.N);
end
function run(c)
arguments
c C
end
for i = 1:c.N
c.x(i) = 1;
end
end
end
end
N_range = 1e4:1e4:1e5;
times = zeros(1, length(N_range));
N_i = 1;
for N = N_range
c = C(N);
tic
c.run();
times(N_i) = toc;
N_i = N_i + 1;
end
plot(times)
  1 Comment
VBBV
VBBV on 21 Mar 2025
@James, Thats possibly due to embedded for loop inside the run function which is being called inside another loop .

Sign in to comment.

Accepted Answer

Matt J
Matt J on 21 Mar 2025
Edited: Matt J on 21 Mar 2025
It is because you are applying property validations {mustBeFloat, mustBeNonnegative} on the x property. This means that in every iteration of the loop in run(), the code has to do the O(N) work of checking whether the entirety of c.x contains non-negative floats.
You will see that the computation becomes both linear and much faster if you modify the classdef to,
properties
N uint32 {mustBeInteger, mustBePositive}
x
end
N_range = 1e4:1e4:1e5;
times = zeros(1, length(N_range));
for i=1:numel(N_range)
N=N_range(i);
c = C(N);
times(i) = timeit(@c.run);
end
plot( times,'bo-');
  13 Comments
Matt J
Matt J on 26 Mar 2025
Seems like the only way, or at least one way, to protect against indexed assignment into c.x with an invalid type would be for c.x itself to be a class instance and that class would have the check built into its subsasgn.
I agree it would be one way, but not the only way. If you provide an overloaded subsasgn for c, it will be called by c.x(i)=rhs and give you access to rhs.
Paul
Paul on 26 Mar 2025
Ah yes. I was too focused on subsasgn as applied to paren indexing on c, which is not applicable here. But now I realize that subsasgn on c will be called on c.x(i) = rhs because of the dot indexing on c, at which point one could enforce constraints on rhs.

Sign in to comment.

More Answers (0)

Categories

Find more on Properties in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!