Global versus Structure of Array
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
댓글 수: 2
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
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 Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!