MATLAB代码段运行计时

xiaoxiao2021-02-28  4

1. tic & toc

代码示例:

tic sum = 0; for x = 1:1e7 sum = sum + x; end t1 = toc %第一次计时 product = 1; for y = 1:1e7 product = product * y; end t2 = toc %第二次计时

显示结果:

t1 =0.0433 t2 =0.0929

2. profile on & profile viewer

相比较上一个来说,这个给出的运行时间更为精细,运行结束会显示每个函数运行的时间,点进每个函数会显示更为详细的每行代码的运行时间,便于进行代码优化。

代码示例:

profile on sum = 0; for x = 1:1e7 sum = sum + x; end product = 1; for y = 1:1e7 product = product * y; end profile viewer

显示结果(与示例代码无关):

转载请注明原文地址: https://www.6miu.com/read-2249983.html

最新回复(0)