如何计算一个可执行文件的运行时间?

How to calculate running time of an executable file?

有什么方法可以计算出可执行文件的准确执行时间吗?

具体来说,我正在寻找一种可用于对使用 Matlab 编译的可执行文件计时的方法。我指的不是 tictoc.

tic-toc 测量 matlab 内部的执行时间。如果您希望您的可执行文件能够提供此信息,您可以提供一个输入参数来指示您的程序 运行 tic-toc 对之间的所有程序。

tic;
%process
toc;

或者,如果您想从外部测量它,则有不同的选项取决于操作系统。例如在 linux 中有命令 time

你总是可以包括

tic

在可执行文件的开头,然后

disp(toc)

最后。

既然你问的是可执行文件的执行时间,我假设你是在命令行环境中工作。

Linux, Unix, Mac OS X 等,你可以使用命令行程序time来测量运行时间。假设您的可执行文件名为 exefile.x,您将输入

time ./exefile.x 

您将获得的输出类似于

real    0m0.419s
user    0m0.112s
sys     0m0.174s

Windows中,有timeit等测量运行时间的工具。有关更多信息,请参见示例 How to measure execution time of command in windows command line?

希望对您有所帮助。

PS:realusersys的解释请参考What do 'real', 'user' and 'sys' mean in the output of time(1)?