Structure array slower than loose variables

조회 수: 12 (최근 30일)
MCH
MCH 2018년 12월 3일
댓글: Bruno Luong 2018년 12월 3일
I need to pass a large number of variables into a number of functions. Passing every variable individually is messy and makes expansion of the code tricky. I used structured arrays to tidy up my variables and package them together into related groups but I have found that this significantly slows down my code (takes approximately 40% longer to execute). I have tried 'unpacking' my data using Method 1 in the following question but this makes no difference.
Loose variables:
output=examplefunction(variable1,variable2,variable3)
Structured:
output=examplefunction(SimulationParameters)
% Unpack data
variable1=SimulationParameters.variable1;
variable2=SimulationParameters.variable2;
variable3=SimulationParameters.variable3;
Are strutures always slower? Are there any other methods for speeding up structures? Official Matlab documentation doesn't provide any guidance on speed. This web page seems to suggest that structures are tidy but slow.
Thanks, M

채택된 답변

Bruno Luong
Bruno Luong 2018년 12월 3일
Yes acessing field names is slow., so try to avoid doing it intensively.
  댓글 수: 2
MCH
MCH 2018년 12월 3일
Thanks Bruno. So the best method for any computationally intensive functions would be to pass loose variables?
Bruno Luong
Bruno Luong 2018년 12월 3일
Can't you reduce the number of variables by putting them for example in an array? What are they?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by