필터 지우기
필터 지우기

Global versus Structure of Array

조회 수: 2 (최근 30일)
balandong
balandong 2016년 10월 5일
편집: Matt J 2016년 10월 6일
For solving ODE using ODE suit, I have to deal with more than 30 constant parameters. The question whether using Global or structure-of-array is the best coding practice to represent the constant parameter.
Most of the advice I read on the net suggest that structure of array is indeed memory efficient. However,from my experience, by using structure-of-array for calculating the derivative inside the ODE suite, MATLAB will re-assess the structure within the array repetitively. Somehow, I guest this will consume the processing time. Second, in term of readability, this will make the code rather long. for example, see the representation in (A) and (B)
(A): structure-of-array Assume we fill the structure s.someval=12; s.de.val=1000;
So, I will write it as
c=time.*s.someval./s.de.val;
Whereas,
(B): Global Assume we define the global parameter someval=12; val=1000; c=time.*someval./val
As you can see, if the equation is complex and long, the equation represent under the structure-of-array compare to global-type will be hard to read and maintain.
May I know, in practice, which is the best approach?
Thanks

채택된 답변

Matt J
Matt J 2016년 10월 5일
편집: Matt J 2016년 10월 5일
Somehow, I guest this will consume the processing time.
Not really.
As you can see, if the equation is complex and long, the equation represent under the structure-of-array compare to global-type will be hard to read and maintain.
To make things neater, you can unpack the variables inside your function before using them,
someval=s.someval;
val=s.de.val;
c=time.*someval./val
I created this tool to help auto-generate the coding when you have lots of variables to unpack.
  댓글 수: 2
Matt J
Matt J 2016년 10월 6일
balandong Commented:
Hi Matt,
Regarding the issue 1) Processing time, I evaluated a model consist of 3 ODE and 3 algebraic equation using ode15s.
The condition tested where
case a) global type of coding e.g., Global alpha delta c=alpha./delta
case b)Equalize long to short notation
e.g., alpha=s.alpha delta= s.delta c=alpha./delta
case c):Straight notation e.g., c=s.alpha./s.delta
The processing time i got for each case was Case a = 1.694 s case b = 2.993 s case c = 3.024 s
or in summary case c> case b> case a
As you can see, the global method is superior even I unpack the variables inside the function.
Matt J
Matt J 2016년 10월 6일
편집: Matt J 2016년 10월 6일
The example may be too small to be representative. As the complexity of the equations increases (you said you had 30 constant parameters in your real application), then the time associated with accessing variables (from global or from struct) should become insignificant compared to the time to perform computations with them.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by