Very Slow Assignment for Clas Properties

조회 수: 9 (최근 30일)
Greg
Greg 2011년 8월 4일
Hi
I have a class with a number of properties. One of the methods for this class which uses these properties is called many times (530,553 times) as part of a sim i run.
Problem is referencing the properties takes up too much cumulative time.
For example I have:
classdef World < handle
properties state = 0;
end
methods
function thisWorld = World()
end
function act(thisWorld)
myState = thisWorld.state; % takes 15 seconds when called 503k end
Another method updates the state property prior to calling the act() method, the values 'state' can be assigned range from 1:1:101.
Please help end
end

답변 (2개)

Daniel Shub
Daniel Shub 2011년 8월 6일
This is a well known problem with MATLAB classes. Unfortunately there is not a good solution that I know of. I would suggest starting at:

Greg
Greg 2011년 8월 15일
Thanks for the response.
It does seem very poor that such a performance hit it incurred when using classes. I'm not a professional programmer but as I see it the use of OO is particularly useful in structuring and maintaining complex code which typically would also be time consuming like sims.
As a result the OO features of the Matlab language are for me redundant.
Are there any plans from Mathworks to address this issue?
Many thanks Greg
  댓글 수: 1
Daniel Shub
Daniel Shub 2011년 8월 15일
It is a problem. Sometimes you can get around the problem by changing how your code works. For example:
N = 503e3;
thisWorld = World;
for ii = 1:N
myState = thisWorld.state;
end
is much faster than
for ii = 1:N
act(thisWorld);
end
obviously not all problems can be solved this way, but sometimes they can be.

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by