I was just curious
If a function needs to return large number of variables does it necessarily take longer to execute? I have a function that calculates values and stores them in arrays. It calculates these values every time it’s called and the number of times it is called has not changed. However now that I have added some return values (each one is a large array with around 1500 double elements, the function returns 8~10 such arrays as opposed to just calculating them) the function seems to take longer to execute (checked using tic before the function call and toc after the function call. Does this cause the total execution time to be more?
I was just curious If a function needs to return large number of variables
Share
In fact it does take longer if you let the function output more arguements. I guess it is related to storing the result.
Here is my testfunction:
And these are the setups that I used to test the speed:
The difference is not huge, but I ran it several times and it does appear to be significant here are the results:
So it will take longer, but I doubt whether the difference is ever interesting enough to outweigh other considerations.