2

Say I have a project which is comprised of:

  • A main script that handles all of the running of my simulation
  • Several smaller functions
  • A couple of structs containing the data

Within the script I will be accessing the functions many times within for loops (some over a thousand times within the minute long simulation). Each function is also looking for data contained with a struct files as part of their calculations, which are usually parameters that are fixed over the course of the simulation, however need to be varied manually between runs to observe the effects.

As typically these functions form the bulk of the runtime I'm trying to save time, as my simulation can't quite run at real-time as it stands (the ultimate goal), and I lose alot of time passing variables/parameters around functions. So I've had three ideas to try and do this:

  1. Load the structs in the main simulation, then pass each variable in turn to the function in the form of a large argument (the current solution).
  2. Load the structs every time the function is called.
  3. Define the structs as global variables.

In terms of both the efficiency of the system (most relevent as the project develops), and possibly as I'm no expert programmer from a "good practise" perspective what is the best solution for this? Is there another option that I have not considered?

3
  • 3
    The first one seems most efficient for me. Commented Dec 23, 2014 at 10:44
  • 1
    Passing and referencing a struct variable is an efficient operation. Look elsewhere to optimize the software. See if a function that you currently call from a loop can be vectorized to process an entire <vector/matrix/etc> at once. Commented Dec 23, 2014 at 13:36
  • It would be helpful to give us some more information about where exactly your code is slowing down. I provided an answer, but given some of what you said in response to matlabgui's answer, I'm unsure if it will help. Commented Dec 23, 2014 at 19:41

2 Answers 2

1

As mentioned above in the comments - the 1st item is best one.

Have you used the profiler to find out where you code takes most of its time?

profile on % run your code profile viewer

Note: if you are modifying your input struct in your child functions -> this will take more time, but if you are just referencing them then that should not be a problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, yes I've been using the profiler alot to understand the processes. As a physicist I'm not well versed in writing particularly efficient or "good" code, and whilst in some places the time is necessary the size my arguments are starting to take is getting daft. If you want specifics a couple of the struct values are changed with one function, then passed back to the rest of the simulation to be read again. That function for varying is quite small though and doesn't affect much, it's the bigger ones that are the bottleneck typically.
0

Matlab does what's known as a "lazy copy" when passing arguments between functions. This means that it passes a pointer to the data to the function, rather than creating a new instance of that data, which is very efficient memory- and speed-wise. However, if you make any alteration to that data inside the subroutine, then it has to make a new instance of that argument so as to not overwrite the argument's value in the main function. Your response to matlabgui indicates you're doing just that. So, the subroutine may be making an entire new struct every time it's called, even though it's only modifying a small part of that struct's values.

If your subroutine is altering a small part of the array, then your best bet is to just pass that small part to it, then assign your outputs. For instance,

[modified_array] = somesubroutine(struct.original_array);
struct.original_array=modified_array;

You can also do this in just one line. Conceptually, the less data you pass to the subroutine, the smaller the memory footprint is. I'd also recommend reading up on in-place operations, as it relates to this.

Also, as a general rule, don't use global variables in Matlab. I have not personally experienced, nor read of an instance in which they were genuinely faster.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.