Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I "profile" just my .m functions in MATLAB??

Tags:

profile

matlab

I have a relatively big code and I would like to use "profile" to improve it. When I use "profile" I got a report listing all the functions (built in's, and my .m functions) and their respective running time.

I would like to list just functions written by myself (not the built in's) and their respective running time. Anyone knows how to do that??

Thanks in advance.

like image 294
Noname Avatar asked Nov 09 '22 14:11

Noname


1 Answers

Here is a try, using the 'info' parameter of the profile function. I am discarding Matlab functions by comparing their complete path name with matlabroot. Here is the code:

profile on

... % Put the code to profile here

% Stop the profiler and get infos
stats = profile('info');

% Sort results based on the total execution time
[~,I] = sort([stats.FunctionTable.TotalTime], 'descend');

for i = I

    % Get file structure
    F = stats.FunctionTable(i);

    % Discard Matlab functions
    if ~isempty(findstr(F.CompleteName, matlabroot))
        continue;
    end

    % Display the total execution time and the function name
    fprintf('%.06f sec\t', F.TotalTime);
    fprintf('%s\n', F.FunctionName);

end

Although the profiler gives a representation that looks much nicer, this does accomplish what you set out to do.

NB: At first I thought I could use the output of the exist function, but only a core subset of Matlab functions are labeled 'builtin'. For instance repmat and verLessThan have the same flag as custom functions, and are therefore not considered to be built-in.

Best,

like image 198
Ratbert Avatar answered Nov 15 '22 07:11

Ratbert