Non-linear scaling for linear increase in complexity

5 visualizaciones (últimos 30 días)
James
James el 20 de Mzo. de 2025
Comentada: Paul el 26 de Mzo. de 2025
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 comentario
VBBV
VBBV el 21 de Mzo. de 2025
@James, Thats possibly due to embedded for loop inside the run function which is being called inside another loop .

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 21 de Mzo. de 2025
Editada: Matt J el 21 de Mzo. de 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 comentarios
Matt J
Matt J el 26 de Mzo. de 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 el 26 de Mzo. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Properties en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by