Is it more efficient to pass an array to a function? Or to generate the (very simple) array for each call?

I have a function that will be called many times (it is used by an ode solver). Is it more time efficient to pass the variables x and y to it, and then generate
linspace(0,x,y)
from within, or to pass linspace(0,x,y) to the function?
Is there a general answer or rule of thumb?

Answers (1)

Passing something as a parameter is the second fastest form of variable access, slower only than accessing a variable previously defined in the same function. It is certainly faster than re-creating the array.

Asked:

on 29 Jun 2017

Commented:

on 18 Jul 2017

Community Treasure Hunt

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

Start Hunting!