Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

cputime

MATLAB 使用的 CPU 时间

说明

示例

t = cputime 返回 MATLAB® 自启动以来使用的总 CPU 时间。返回的 CPU 时间以秒为单位表示。

cputime 的每次调用都会返回 MATLAB 在调用该函数之前使用的总 CPU 时间。要测量用于运行您的代码的 CPU 时间,请在该代码之前和之后分别调用一次 cputime,然后计算返回值之间的差值。

示例

全部折叠

测量运行 surf(peaks(40)) 所用的 CPU 时间。

tStart = cputime;
surf(peaks(40))

Figure contains an axes object. The axes object contains an object of type surface.

tEnd = cputime - tStart
tEnd = 1.2800

提示

  • 要衡量代码的性能,请使用 timeittictoc 函数。与测量 CPU 时间的 cputime 函数不同,timeittic/toc 返回挂钟时间。

    例如,pause 的 CPU 时间通常很小:

    tStart = cputime;
    pause(1)
    tEnd = cputime - tStart
    
    tEnd =
    
        0.1094
    

    但是,挂钟时间会考虑暂停 MATLAB 执行的实际时间:

    tic
    pause(1)
    toc
    Elapsed time is 1.000483 seconds.
    

    有关详细信息,请参阅衡量代码的性能

扩展功能

版本历史记录

在 R2006a 之前推出

另请参阅

| |