Array performance in class attributs

조회 수: 2 (최근 30일)
Franck AUBINEAU
Franck AUBINEAU 2024년 2월 5일
댓글: Franck AUBINEAU 2024년 2월 5일
Here is an (short) example of the observed problem. It is not the exact code but it is the exact problem :-)
% Class
classdef MaClasse < handle
properties (Access = public)
tabVal = [];
end
end
% Code, declaration, allocation
pTest = MaClasse();
pTest.tabVal = zeros(200,1000);
tmp = zeros(200,1000);
% Problem
pTest.tabVal(10,:) = ones(1,1000); %
tmp(10,:) = ones(1,1000); % -> twice even more faster than the line above

채택된 답변

Shivam
Shivam 2024년 2월 5일
Hi Franck,
Based on the code you have shared, it seems you want to know the discrepancy in execution time between assigning a matrix of ones to a property of a custom class object and assigning it directly to a standard array.
It is important to know that the observed difference in performance comes from MATLAB's additional processing when dealing with class properties. Whenever you alter a property of a handle class, MATLAB has to conduct extra operations, including managing object references and maintaining the integrity and encapsulation of the data.
And, with a conventional matrix such as tmp, MATLAB can interact with and alter the memory directly, bypassing the complexities introduced by object-oriented features.
I hope it helps in understanding the performance difference between the two lines.
Thanks
  댓글 수: 1
Franck AUBINEAU
Franck AUBINEAU 2024년 2월 5일
Hi and thanks for this answer.
It's exactly what I think but I'm very disapointed by such a difference. I'm using Matlab class in my simulation code and it seems that performance (this example and others like access time in handle cell array) are very slow.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by