From: Etienne on
Hi Folks

I am using a mexCallMatlab statement to call a matlab function, and within this function I am calling subfunctions from a case switchyard (see below). I am finding that when I split the function into its subfunctions, and save each subfunction in its own file, and then call this function directly from mexcallmatlab, I am cutting the run time in half. I did not expect such a massive improvement. I would prefer to keep the original format for the sake of neatness and not having a load of files all over the place. Can anybody exlplain why we have such massive speed improvments, or can you maybe suggest how I can alter the switchyard below?

Thanks in advance

Etienne

========================================
function varargout=AUTOEQN(fstr,varargin)

switch fstr
case 'FUNC' % Function values called from AUTO
[varargout{1:2}]=FUNC(varargin{1:3});
case 'STPN' % Obtain initial conditions
[varargout{1:3}]=STPN;
case 'BCND' % Boundary value problem
[varargout{1:2}]=BCND(varargin{1:3});
case 'ICND'
return
case 'FOPT' % Optimization problem
return
case 'PVLS'
return
otherwise
error('AUTOmatlab:eqnFileFuncCall',...
'Incorrect function call from mex file.');
end
From: Jan Simon on
Dear Etienne!

> I am using a mexCallMatlab statement to call a matlab function, and within this function I am calling subfunctions from a case switchyard (see below). I am finding that when I split the function into its subfunctions, and save each subfunction in its own file, and then call this function directly from mexcallmatlab, I am cutting the run time in half. I did not expect such a massive improvement. I would prefer to keep the original format for the sake of neatness and not having a load of files all over the place. Can anybody exlplain why we have such massive speed improvments, or can you maybe suggest how I can alter the switchyard below?
> Etienne
>

> function varargout=AUTOEQN(fstr,varargin)
>
> switch fstr
> case 'FUNC' % Function values called from AUTO
> [varargout{1:2}]=FUNC(varargin{1:3});
> case 'STPN' % Obtain initial conditions
> [varargout{1:3}]=STPN;
> case 'BCND' % Boundary value problem
> [varargout{1:2}]=BCND(varargin{1:3});
> case 'ICND'
> return
> case 'FOPT' % Optimization problem
> return
> case 'PVLS'
> return
> otherwise
> error('AUTOmatlab:eqnFileFuncCall',...
> 'Incorrect function call from mex file.');
> end

VARGIN and VARARGOUT can need a noticable chunk of time. You can use a fixed list of inputs also:
function [Out1, Out2, Out3] = AUTOEQN(fstr, In1, In2, In3)
switch fstr
case 'FUNC' % Function values called from AUTO
[Out1, Out2] = FUNC(In1, In2, In3);
case 'STPN' % Obtain initial conditions
[Out1, Out2, Out3] = STPN;
...

While for Matlab 6.5 the SWITCH case was optimal, it seems that in Matlab 7.8 this is faster:
if strcmp(fstr, 'FUNC')
elseif strcmp(fstr, 'STPN')
...
But I'm not sure about this and the effect is tiny.

Another idea would be to let the swicthyard reply a list of function handles to the mex file, which call them directly using the FEVAL function in mexCallMATLAB.

Kind regards, Jan
From: Etienne on
Thanks Jan

I tried to get the function handles to work, but am struggling. I'll try some other options. I just wish there were loads more fortran examples!

Regards

Etienne
 | 
Pages: 1
Prev: KK-NearestNeighbour
Next: speaker recognition