Speed performance between class, struct and local variable

조회 수: 11 (최근 30일)
Franck AUBINEAU
Franck AUBINEAU 2024년 2월 7일
댓글: Matt J 2024년 2월 8일
Here is the illustration of my problem :
% Class
classdef MaClasse < handle
properties (Access = public)
vec1 = [];
vec2 = [];
vec3 = [];
end
end
...
nb = 50000;
randstream = Randstream('mrg32k3a');
% Cas 1
pObj = MaClasse();
pObj.vec1 = randn(randstream,100,1000);
pObj.vec2 = randn(randstream,100,1000);
pObj.vec3 = randn(randstream,100,1000);
tic
for i=1:nb
pObj.vec1 = pObj.vec1.*pObj.vec2 + pObj.vec3;
end
toc
% ----> 6.80 seconds
% Cas 2
vec1 = randn(randstream,100,1000);
vec2 = randn(randstream,100,1000);
vec3 = randn(randstream,100,1000);
tic
for i=1:nb
vec1 = vec1.*vec2 + vec3;
end
toc
% ----> 1.61 seconds
% Cas 3
str.vec1 = randn(randstream,100,1000);
str.vec2 = randn(randstream,100,1000);
str.vec3 = randn(randstream,100,1000);
tic
for i=1:nb
str.vec1 = str.vec1.*str.vec2 + str.vec3;
end
toc
% ----> 6.95 seconds
A difference of factor 4 seems critical to use Matlab with POO (even with struct according to my test)
If someone has a solution but not to say using local variable and then copy into attributs ;-)

채택된 답변

Matt J
Matt J 2024년 2월 7일
편집: Matt J 2024년 2월 7일
Well Case 1 and Case 3 have more indexing operations, so it makes sense that that overhead should dominate assuming the + and .* operations are sufficiently fast. That seems to be the case, because below we see that once the data size is sufficiently large, the relative differences in performance diminish. It just shows how fast .* and + are for small/medium data sizes!
rs = RandStream('mrg32k3a');
M=5000; N=5000;
% Cas 1
pObj = MaClasse();
pObj.vec1 = randn(rs,M,N);
pObj.vec2 = randn(rs,M,N);
pObj.vec3 = randn(rs,M,N);
str.vec1 = randn(rs,M,N);
str.vec2 = randn(rs,M,N);
str.vec3 = randn(rs,M,N);
vec1 = randn(rs,M,N);
vec2 = randn(rs,M,N);
vec3 = randn(rs,M,N);
timeit( @() pObj.vec1.*pObj.vec2 + pObj.vec3 )
ans = 0.0732
timeit( @() str.vec1.*str.vec2 + str.vec3 )
ans = 0.0726
timeit( @() vec1.*vec2 + vec3 )
ans = 0.0663
  댓글 수: 2
Franck AUBINEAU
Franck AUBINEAU 2024년 2월 8일
"Well Case 1 and Case 3 have more indexing operations, so it makes sense that that overhead should dominate assuming the + and .* operations are sufficiently fast."
It means that if you're using class in a complex simulation calculation code with Matlab, you have to accept that it will be less efficient several simple assembled code witj local variable unless your data are sufficiently large or adapted to your code.
I thought that, with pre allocation, there won't be such a difference.
Matt J
Matt J 2024년 2월 8일
It means that if you're using class in a complex simulation calculation code with Matlab, you have to accept that it will be less efficient
I don't think it means it's less efficient just because it takes longer. It takes longer because you are giving it more work.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by