I am facing serious performance issues with anonymous functions in matlab 2011a, where the overhead introduced by the anonymous container function is much larger than the time taken by the private function itself.
I read several related questions in which users explained with caution that this is a problem that others are experiencing, that I can significantly increase performance by getting rid of anonymous containers. Unfortunately, my code is structured in such a way that I am not sure how to do this without breaking a lot of things.
So, are there any workarounds to improve the performance of anonymous functions without completely eliminating them or developing templates that would allow me to do away with them without bloating my code and without spending a lot of time reorganizing?
Some information that may help:
The following is a collection of anonymous functions that are stored as a class property. Using an int array, which, in turn, is used by the switch statement, can replace the array in principle, but the contents of the GPs can be changed - there are other functions with the same argument structure as the traces used in it - and the contents of the GPs in some cases can be determined at runtime.
m3.GPs = {@(X,ytrain,xStar,noisevar,params)traingp(X,ytrain,xStar,noisevar,1,params,[1 0]'); @(X,ytrain,xStar,noisevar,params)traingp(X,ytrain,xStar,noisevar,1,params,[-1 1]'); @(X,ytrain,xStar,noisevar,params)traingp(X,ytrain,xStar,noisevar,2,params,0); @(X,ytrain,xStar,noisevar,params)traingp(X,ytrain,xStar,noisevar,3,params,0); @(X,ytrain,xStar,noisevar,params)traingp(X,ytrain,xStar,noisevar,4,params,[0 0 0]')};
Later, the GPs elements are called by a member function of the class, for example:
GPt = GPs{t(j)}(xj,yj,gridX(xi),thetaT(1),thetaT(2:end));
According to the profiler, the battery life for anonymous wrappers is 95% of the total time (1.7 seconds for 44 calls!) Versus 5% for the function containing the data. I use a similar approach elsewhere, where the cost of anonymous wrappers is even greater, proportionally.
Does anyone have any thoughts on how to reduce the overhead of anonymous calls or, if they are not available, how to replace the anonymous function while maintaining the flexibility they provide (rather than introducing a bunch of additional bookkeeping and passing arguments)?
Thanks!