# Question to MATLAB profiler and speed of code (spline calculation during optimization)

4 views (last 30 days)

Show older comments

Hello dear MATLAB community,

I have a question concerning this piece of code

This will calculate a piecewise cubic spline for my parametrized function. It is calles so often because it´s inside an optimization routine (fmincon). My first question is: Is there a way to speed up this code? And the second question: Why is the runtime of line 11 and 13 and 12 and 14 so different? Both lines are doing the same operation but the the calculation time differs around 30%?

Thanks for your help,

David

##### 3 Comments

Sean de Wolski
on 31 Oct 2014

### Answers (5)

Stephen23
on 30 Oct 2014

Edited: Stephen23
on 25 Nov 2014

Two questions... two answers.

1) Probably. But this depends mostly on the two functions griddedInterpolant and F1 / F3, about which you have not provided us with any information. For example it might be possible to call these functions only once each, if griddedInterpolant can accept a matrix for its second input. It might also be faster to interpolate the data directly, without creating the intermediate functions F1 / F3.

2) MATLAB is an interpreted language with some sophisticated memory and data management. I am certainly no expert on this topic (someone from MATLAB could help you), but this apparently includes optimization of functions when they are first called, allocating memory and checking if the data changes during the code. These first steps add a little time when code is first run, or a function is first called. Those times look fine.

##### 7 Comments

Sean de Wolski
on 30 Oct 2014

Edited: Sean de Wolski
on 30 Oct 2014

Marco
on 30 Oct 2014

Edited: Marco
on 30 Oct 2014

EDIT: Please ignore this answer, I realized that my answer wasn´t a good answer, because lines 11 and 13 actually do not differ in respect to row and column access.

The answer was: About the difference in calculation speed in lines 11 and 13, I would expect that this has to do with the way how MATLAB stores the data in the memory of the hardware. It is said for MATLAB, that data stored in columns can consecutively be accessed faster by the hardware than the harware could consecutively access data being stored in rows. This is how The Mathworks implemented MATLAB. For other software this could be the opposite round. It depends on the specific low level implementation of the software. Here is a link to some MATLAB information about this: programming-patterns-maximizing-code-performance-by-optimizing-memory-access

##### 0 Comments

Sean de Wolski
on 30 Oct 2014

Edited: Sean de Wolski
on 30 Oct 2014

You can do this with one call to griddedInterpolant or interp1 (which builds a griddedInterpolant under the hood). Build it once and evaluate it on [x2 x2]. Since splines are calculated in one dimension anyway.

The call to interp1 would look like this:

% Simulated data

xges = cumsum(rand(10,2));

xi = 1:10; % index

xq = 1:0.1:10; % query

% x2 is first column, y2 is second

x2y2 = interp1(xi.',xges,xq.'); % interp

##### 5 Comments

Sean de Wolski
on 30 Oct 2014

Run each a few more times. Is there anything stochastic about the system/objective function/etc.?

Philip Borghesani
on 30 Oct 2014

Your attempted pre-allocation by calling zeros for xj and yj is not doing anything useful and may be costing a small amount of time try just a simple assignment and removing the calls to zeros:

xj=F1(x2);

yj=F3(x2);

There is never a benefit to preallocating something if a following assignment is going to write into the entire variable.

##### 1 Comment

Sean de Wolski
on 31 Oct 2014

Interior point is usually the best algorithm (hence why we made it the default!).

I question if the spline calculations are highly unstable (a trait of splines) and that this could be causing the optimizer to have trouble. This being the reason for the huge number of function evaluations.

Also, it wouldn't surprise me at all if the actual display iter setting is causing much of the time as printing to the command line is slower than computation. Try turning that off and rerunning.

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!