Function call counting in Matlab

Here’s a neat way to keep track of the cost, i.e. number of function calls in Matlab: rather than using a counter variable and increment it each time a function is called, use the internal Matlab counters, accessible through the FunctionTable struct array.

The loop below is necessary only to get the index of the desired function in the struct array (If you know a better way of looking up a function in FunctionTable, let me know!).

Replace the function name ftest if needed.

profile on

% your code comes here
% (cost is counted from "profile on" to "profile off")

p = profile('info');
for i = 1:size(p.FunctionTable,1)
    if strcmp(p.FunctionTable(i).FunctionName,'ftest')
        break
    end
end
cost = p.FunctionTable(i).NumCalls;
profile off
This entry was posted in Scientific Computing, Tips & Tricks and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>