【问题标题】:How can I "profile" just my .m functions in MATLAB??如何在 MATLAB 中“分析”我的 .m 函数?
【发布时间】:2015-05-08 14:10:01
【问题描述】:

我有一个比较大的代码,我想使用“配置文件”来改进它。当我使用“配置文件”时,我得到了一份报告,其中列出了所有函数(内置函数和我的 .m 函数)及其各自的运行时间。

我想仅列出我自己编写的函数(不是内置函数)及其各自的运行时间。有人知道怎么做吗??

提前致谢。

【问题讨论】:

    标签: matlab profile


    【解决方案1】:

    这是一个尝试,使用profile函数的'info'参数。我通过将它们的完整路径名与matlabroot 进行比较来丢弃 Matlab 函数。代码如下:

    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
    

    虽然分析器提供的表示看起来更好,但这确实完成了您打算做的事情。

    NB:起初我以为我可以使用exist 函数的输出,但只有Matlab 函数的核心子集被标记为“内置”。例如repmatverLessThan 与自定义函数具有相同的标志,因此不被视为内置函数。

    最好的,

    【讨论】:

      【解决方案2】:

      您在查看个人资料摘要吗?如果是这样,那么就会有一列列出每个功能(包括您自己的功能)的总时间自用时间。我认为自我时间可能是您正在寻找的时间。此外,您可以单击最左侧列中的函数名称,查看代码中时间花费的详细信息。请务必选中显示函数列表框并刷新以查看每行代码的计时详细信息。我希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2012-06-28
        • 1970-01-01
        • 1970-01-01
        • 2012-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-21
        • 1970-01-01
        相关资源
        最近更新 更多