Question

I have a function where a lot of the work can be precomputed. The precomputation takes around 2000 microseconds and produces five arrays, the largest of which has length 360919, so clearly it is impractical to include them in the source code.

After precomputing, the function takes around 70 microseconds, so I get about a 30x speedup by doing the precomputation. I need to call this function millions of times (as part of a Monte Carlo algorithm) so it's obviously beneficial to have it run as fast as possible.

What is the best way to precompute the necessary arrays? At the moment I'm looking at the following options

  1. Use persistent variables, and precompute them the first time the function is called.

  2. Use global variables, and initialize them before calling the function.

  3. Use a class, and precompute the arrays when the object is initialized.

All of these have some level of overhead, compared to the obvious but non-composable solution of inlining everything so that the local variables and precomputed variables are in the same workspace.

Is there another solution I've missed, that has less overhead than the above methods, but retains composability? Is the best solution just to drop into C?

Was it helpful?

Solution

Another possibility, which I use quite often: use an auxiliary script (m-file) instead of a function. The main script does the precomputing and then calls the auxiliary script.

The auxiliary script has visibility of all variables defined in the calling script, so you don't need global or persistent variables, which probably introduce some overhead in terms of running time.

Of course, you need to take care that the variables in the auxiliary script do not overwrite anything they shouldn't in the main script. That is, you need to prevent clashes in the names of the variables between the two scripts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top