简体   繁体   中英

How can I “profile” just my .m functions in 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.

Are you looking at the Profile Summary ? If so, then there is a column listing total time and self time for each function (including your own). I think self time may be the time you are looking for. Also, you can click on the function name in the left most column and see the details of how the time was spent in the code. Be sure to check the Show Function listing box and refresh to see the timing detail on each line of the code. I hope this helps.

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM