Clear Filters
Clear Filters

Global versus Structure of Array

1 view (last 30 days)
balandong
balandong on 5 Oct 2016
Edited: Matt J on 6 Oct 2016
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

Accepted Answer

Matt J
Matt J on 5 Oct 2016
Edited: Matt J on 5 Oct 2016
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 Comments
Matt J
Matt J on 6 Oct 2016
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 on 6 Oct 2016
Edited: Matt J on 6 Oct 2016
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!