Matlab中的时序代码 [英] timing code in matlab

查看:268
本文介绍了Matlab中的时序代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用4种不同的方式写下了一个函数,我想给它计时.

I have written down a function in 4 different ways and I want to time it .

到目前为止,我一直在做此事:

Up-to now I have been doing this thing :

tic
%//function 1
toc

tic
%//function 2
toc

tic
%//function 3
toc

tic
%//function 4
toc

但是现在我想计算每个功能的时序数据(例如100次),然后计算在每个功能上花费的平均时间.我该怎么办?

But now I want to compute the timing data for each function for (say 100 times) each and then compute the average time spent on each function. How can I do so?

我还读到某个地方,打印的时间是经过的挂钟"时间-因此,它会受到MATLAB程序运行时我的计算机在执行的其他任何操作的影响.

Also I read somewhere that the time printed is the elapsed "wall clock" time – so it will be affected by whatever else my computer is doing whilst the MATLAB program was running.

那么有没有更好的方法呢?我听说有一个内置MATLAB的code-profiler,其中包含"profile on"命令.请问有人可以建议我使用它的方式吗?

So is there a better way of doing it ?? I have heard there is a MATLAB built in code-profiler with the command "profile on". Please can anyone suggest me the way in which I can use it?

我还咨询了以下站点:定时MATLAB中的代码 Profiler查找代码瓶颈.

I have also consulted the sites : Timing code in MATLAB and Profiler to find code bottlenecks.

请建议如何循环执行多次.提前致谢.

Please suggest how to do this many times in a loop. Thanks in advance.

推荐答案

计时MATLAB代码的最佳方法是使用

The best way to time MATLAB code is to use timeit, available from the MATLAB Central File Exchange.

它是由MathWorks的高级开发人员之一Steve Eddins实现的,它在计时代码时会考虑很多细微之处.例如,在函数而不是脚本中执行代码时,代码的运行方式有很大不同,并且需要两次预热"运行才能充分利用JIT编译器.它还将循环运行代码多次,并取中位数.

It was implemented by Steve Eddins, one of the senior developers at MathWorks, and it takes care of a lot of subtleties in timing your code. For example, code runs very differently when it's executed within a function rather than within a script, and it needs to have a couple of "warm-up" runs in order to take proper advantage of the JIT compiler. It will also run the code many times in a loop, and take the median.

在不了解MATLAB幕后工作原理的情况下很难解决这些问题,并且timeit会为您处理这些事情-tictoc的简单应用不会.

These things are difficult to get right without knowing a fair amount about how MATLAB works under the hood, and timeit takes care of these things for you - simple applications of tic and toc do not.

正如其他答案所建议的那样,使用探查器是有问题的,因为它会关闭JIT编译器的许多方面,并且不会以正常的速度运行. Profiler做得很好,可以告诉您代码的哪些部分需要相对较大的时间比例,即发现瓶颈,但这并不是要为您提供实际的时间安排.

Using the profiler, as other answers have suggested, is problematic as it switches off many aspects of the JIT compiler, and will not run at the same speed as it does normally. Profiler does a great job of telling you which portions of your code take a relatively large proportion of time, i.e. discovering bottlenecks, but it's not intended for giving you actually realistic timings.

请注意,在最新版本(R2013b)中,timeit作为核心MATLAB的一部分提供,不需要从File Exchange获取.

Note that in the most recent version (R2013b), timeit is available as part of core MATLAB, and does not need to be obtained from the File Exchange.

例如,要将输入参数x等于64的函数one计时,您可以输入:

For example, to time your function one with the input argument x equal to 64, you would type:

myfun = @()one(64);
timeit(myfun);

这是为您的函数one创建一个函数句柄(确保代码在函数内部执行,如上所述),然后将该函数句柄传递给timeit.输出是timeit对执行代码所花费时间的估计.

What this does is to make a function handle to your function one (which makes sure that the code is executed inside a function, important as mentioned above), then passes this function handle into timeit. The output is timeit's estimate of the time taken to execute the code.

这篇关于Matlab中的时序代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆