Non-linear scaling for linear increase in complexity
    5 ビュー (過去 30 日間)
  
       古いコメントを表示
    
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 件のコメント
  VBBV
      
      
 2025 年 3 月 21 日
				@James, Thats possibly due to embedded for loop inside the run function which is being called inside another loop . 
採用された回答
  Matt J
      
      
 2025 年 3 月 21 日
        
      編集済み: Matt J
      
      
 2025 年 3 月 21 日
  
      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 件のコメント
  Matt J
      
      
 2025 年 3 月 26 日
				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
      
      
 2025 年 3 月 26 日
				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. 
その他の回答 (0 件)
参考
カテゴリ
				Help Center および File Exchange で Properties についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




