Question to MATLAB profiler and speed of code (spline calculation during optimization)
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Sean de Wolski
2014년 10월 31일
Interior point is usually the best. I question of the spline calculations are highly unstable (a trait of splines) that could be causing the optimizer to have trouble.
답변 (5개)
Stephen23
2014년 10월 30일
편집: Stephen23
2014년 11월 25일
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
Sean de Wolski
2014년 10월 30일
편집: Sean de Wolski
2014년 10월 30일
How many times do you have to run this? I mean at 0.000019s it'll have to run 52000x to take a second.
Marco
2014년 10월 30일
편집: Marco
2014년 10월 30일
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
Sean de Wolski
2014년 10월 30일
편집: Sean de Wolski
2014년 10월 30일
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
Sean de Wolski
2014년 10월 30일
Run each a few more times. Is there anything stochastic about the system/objective function/etc.?
Philip Borghesani
2014년 10월 30일
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.
Sean de Wolski
2014년 10월 31일
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!