柏睿网络做机房下送风优势建议有什么优势?

下载作业帮安装包
扫二维码下载作业帮
1.75亿学生的选择
matlab中tol是什么意思?tic和toc是什么意思?
tol是绝对误差限,是tolerance的缩写,默认值是1e-6.今天看quad函数看到的.
Q = QUAD(FUN,A,B,TOL) uses an absolute error tolerance of TOL instead of the default, which is 1.e-6.
为您推荐:
其他类似问题
扫描下载二维码下载作业帮安装包
扫二维码下载作业帮
1.75亿学生的选择
MATLAB 中 TIC TOC 是那几个英语单词的缩写
估计是Time In Count 和 Time Out Count
temperature indicating controllingTheory of Constraints
为您推荐:
扫描下载二维码[转载]关于MATLAB中的tic toc的问题
已有 12239 次阅读
|个人分类:|系统分类:|关键词:matlab
计算时间|文章来源:转载
关于MATLAB中的tic toc的问题 13:42:06|&分类:&默认分类&|&标签:&|字号大中小&订阅其一)MATLAB实际单位时间计时函数的具体应用,在编写程序时,经常需要获知代码的执行实际时间,这就需要在程序中用到计时函数,matlab中提供了以下三种方法:1.cputime(单位不明)返回matlab启动以来的CPU时间,可以在程序执行钱保存当时的CPU时间,然后在程序执行结束后用cputime减去运行前保存的数值,就可以获取程序的实际运行时间&&t0=pause(3);TimeCost=cputime-t02.tic/toc(单位s)tic用在程序的开始,作用是启动一个计时器,然后在程序尾部放一个toc,表示终止计时器,并返回tic启动以来的总时s间3.etime(单位s)etime(t1,t2)用来计算两个日期向量t1和t2之间的时间差,结合前面讲到的clock函数也可以用来确定程序代码的运行时间&&t0=pause(3);TimeCost=etime(clock,t0)在三种计时中建议使用第二种,相对来说最精确。当然你可以使用profiler来确定你的执行时间,并且具体到没有个命令的时间经常我们需要计算我们程序到底运行多长时间,这样可以比较程序的执行效率。当然这个对于只有几秒钟的小程序没有什么意义,但是对于大程序就有很重要的意义了。下面我们就说说Matlab中计算程序运行时间的三种常用方法吧!注意:三种方法由于使用原理不一样,得到结果可能有一定 的差距!(其二)1、tic和toc组合(使用最多的)计算tic和toc之间那段程序之间的运行时间,它的经典格式为tic。。。。。。。。。。toc复制代码换句话说程序,程序遇到tic时Matlab自动开始计时,运行到toc时自动计算此时与最近一次tic之间的时间。这个有点拗口,下面我们举个例子说明% by dynamic of Matlab技术论坛% see also % contact me %
12:08:47clc%tic1t1=for i=1:3%tic2t2=pause(3*rand)%计算到上一次遇到tic的时间,换句话说就是每次循环的时间disp(['toc计算第',num2str(i),'次循环运行时间:',num2str(toc)]);%计算每次循环的时间disp(['etime计算第',num2str(i),'次循环运行时间:',num2str(etime(clock,t2))]);%计算程序总共的运行时间disp(['etime计算程序从开始到现在运行的时间:',num2str(etime(clock,t1))]);disp('======================================')end%计算此时到tic2的时间,由于最后一次遇到tic是在for循环的i=3时,所以计算的是最后一次循环的时间disp(['toc计算最后一次循环运行时间',num2str(toc)])disp(['etime程序总运行时间:',num2str(etime(clock,t1))]);复制代码运行结果如下,大家可以自己分析下toc计算第1次循环运行时间:2.5628etime计算第1次循环运行时间:2.562etime计算程序从开始到现在运行的时间:2.562======================================toc计算第2次循环运行时间:2.8108etime计算第2次循环运行时间:2.813etime计算程序从开始到现在运行的时间:5.375======================================toc计算第3次循环运行时间:2.0462etime计算第3次循环运行时间:2.046etime计算程序从开始到现在运行的时间:7.421======================================toc计算最后一次循环运行时间2.0479etime程序总运行时间:7.421复制代码2、etime(t1,t2)并和clock配合来计算t1,t2之间的时间差,它是通过调用windows系统的时钟进行时间差计算得到运行时间的,应用的形式t1=。。。。。。。。。。。t2=etime(t2,t1)复制代码至于例子我就不举了,因为在上面的例子中使用了etime函数了3、cputime函数来完成使用方法和etime相似,只是这个是使用cpu的主频计算的,和前面原理不同,使用格式如下t0=cputime。。。。。。。。。。。。。t1=cputime-t0复制代码上面说到了三种方法,都是可以进行程序运行时间计算的,但是Matlab官方推荐使用tic/toc组合,When timing the duration of an event, use the tic and toc functions instead of clock or etime.至于大家可以根据自己的喜好自己选择,但是使用tic/toc的时候一定要注意,toc计算的是与最后一次运行的tic之间的时间,不是第一个tic,更不是第二个。。。。。-----------------------------------------------------------------------------------------------------------------------------------------------------------------------TIC开始一个跑表计时器。通过TIC,可以得到使用TIC命令至使用TOC命令两者之间所消耗的时间Tstart=TIC 将时间保存为一个输出参数Tstart.TSTART的数值只有作为在随后出现的TOC调用的输入参数时有用。例如:测量Bessel函数的最小和平均时间:REPS = 1000; minTime = I nsum = 10;for i=1:REPStstart =sum = 0; for j=1:nsum, sum = sum + besselj(j,REPS); endtelapsed = toc(tstart);minTime = min(telapsed,minTime);关于MATLAB中的tic toc的问题 13:42:06|&分类:&默认分类&|&标签:&|字号大中小&订阅其一)MATLAB实际单位时间计时函数的具体应用,在编写程序时,经常需要获知代码的执行实际时间,这就需要在程序中用到计时函数,matlab中提供了以下三种方法:1.cputime(单位不明)返回matlab启动以来的CPU时间,可以在程序执行钱保存当时的CPU时间,然后在程序执行结束后用cputime减去运行前保存的数值,就可以获取程序的实际运行时间&&t0=pause(3);TimeCost=cputime-t02.tic/toc(单位s)tic用在程序的开始,作用是启动一个计时器,然后在程序尾部放一个toc,表示终止计时器,并返回tic启动以来的总时s间3.etime(单位s)etime(t1,t2)用来计算两个日期向量t1和t2之间的时间差,结合前面讲到的clock函数也可以用来确定程序代码的运行时间&&t0=pause(3);TimeCost=etime(clock,t0)在三种计时中建议使用第二种,相对来说最精确。当然你可以使用profiler来确定你的执行时间,并且具体到没有个命令的时间经常我们需要计算我们程序到底运行多长时间,这样可以比较程序的执行效率。当然这个对于只有几秒钟的小程序没有什么意义,但是对于大程序就有很重要的意义了。下面我们就说说Matlab中计算程序运行时间的三种常用方法吧!注意:三种方法由于使用原理不一样,得到结果可能有一定 的差距!(其二)1、tic和toc组合(使用最多的)计算tic和toc之间那段程序之间的运行时间,它的经典格式为tic。。。。。。。。。。toc复制代码换句话说程序,程序遇到tic时Matlab自动开始计时,运行到toc时自动计算此时与最近一次tic之间的时间。这个有点拗口,下面我们举个例子说明% by dynamic of Matlab技术论坛% see also % contact me %
12:08:47clc%tic1t1=for i=1:3%tic2t2=pause(3*rand)%计算到上一次遇到tic的时间,换句话说就是每次循环的时间disp(['toc计算第',num2str(i),'次循环运行时间:',num2str(toc)]);%计算每次循环的时间disp(['etime计算第',num2str(i),'次循环运行时间:',num2str(etime(clock,t2))]);%计算程序总共的运行时间disp(['etime计算程序从开始到现在运行的时间:',num2str(etime(clock,t1))]);disp('======================================')end%计算此时到tic2的时间,由于最后一次遇到tic是在for循环的i=3时,所以计算的是最后一次循环的时间disp(['toc计算最后一次循环运行时间',num2str(toc)])disp(['etime程序总运行时间:',num2str(etime(clock,t1))]);复制代码运行结果如下,大家可以自己分析下toc计算第1次循环运行时间:2.5628etime计算第1次循环运行时间:2.562etime计算程序从开始到现在运行的时间:2.562======================================toc计算第2次循环运行时间:2.8108etime计算第2次循环运行时间:2.813etime计算程序从开始到现在运行的时间:5.375======================================toc计算第3次循环运行时间:2.0462etime计算第3次循环运行时间:2.046etime计算程序从开始到现在运行的时间:7.421======================================toc计算最后一次循环运行时间2.0479etime程序总运行时间:7.421复制代码2、etime(t1,t2)并和clock配合来计算t1,t2之间的时间差,它是通过调用windows系统的时钟进行时间差计算得到运行时间的,应用的形式t1=。。。。。。。。。。。t2=etime(t2,t1)复制代码至于例子我就不举了,因为在上面的例子中使用了etime函数了3、cputime函数来完成使用方法和etime相似,只是这个是使用cpu的主频计算的,和前面原理不同,使用格式如下t0=cputime。。。。。。。。。。。。。t1=cputime-t0复制代码上面说到了三种方法,都是可以进行程序运行时间计算的,但是Matlab官方推荐使用tic/toc组合,When timing the duration of an event, use the tic and toc functions instead of clock or etime.至于大家可以根据自己的喜好自己选择,但是使用tic/toc的时候一定要注意,toc计算的是与最后一次运行的tic之间的时间,不是第一个tic,更不是第二个。。。。。-----------------------------------------------------------------------------------------------------------------------------------------------------------------------TIC开始一个跑表计时器。通过TIC,可以得到使用TIC命令至使用TOC命令两者之间所消耗的时间Tstart=TIC 将时间保存为一个输出参数Tstart.TSTART的数值只有作为在随后出现的TOC调用的输入参数时有用。例如:测量Bessel函数的最小和平均时间:REPS = 1000; minTime = I nsum = 10;for i=1:REPStstart =sum = 0; for j=1:nsum, sum = sum + besselj(j,REPS); endtelapsed = toc(tstart);minTime = min(telapsed,minTime);endaverageTime = toc/REPS;--------------------------------------------------------------------------------------------------------------------------------------------------------------------------如何将tic toc处理过的时间顺序的存储到矩阵中? 因为我发现每处理一次就会覆盖掉上次的时间而我需要将每次处理的时间都记录下来希望哪位高手能帮忙指导一下。可以先分配一个矩阵,然后每运行一次将值存入.A=ones(m,n)%其中m,n是你处理为次数;for i=1..numtic,%你的程序;a=A(i)=a;end这样最后的时间就在A中了.endaverageTime = toc/REPS;--------------------------------------------------------------------------------------------------------------------------------------------------------------------------如何将tic toc处理过的时间顺序的存储到矩阵中? 因为我发现每处理一次就会覆盖掉上次的时间而我需要将每次处理的时间都记录下来希望哪位高手能帮忙指导一下。可以先分配一个矩阵,然后每运行一次将值存入.A=ones(m,n)%其中m,n是你处理为次数;for i=1..numtic,%你的程序;a=A(i)=a;end这样最后的时间就在A中了.
转载本文请联系原作者获取授权,同时请注明本文来自陈永盛科学网博客。链接地址:
上一篇:下一篇:
当前推荐数:0
评论 ( 个评论)
扫一扫,分享此博文
作者的其他最新博文
热门博文导读
Powered by
Copyright &matlab - tic, toc functions analog in Python - Stack Overflow
to customize your list.
Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
J it only takes a minute:
What is the best analog of MATLAB tic and toc functions ( ) in Python?
Apart from timeit which ThiefMaster mentioned, a simple way to do it is just (after importing time):
t = time.time()
# do stuff
elapsed = time.time() - t
I have a helper class I like to use:
class Timer(object):
def __init__(self, name=None):
self.name = name
def __enter__(self):
self.tstart = time.time()
def __exit__(self, type, value, traceback):
if self.name:
print '[%s]' % self.name,
print 'Elapsed: %s' % (time.time() - self.tstart)
It can be used as a context manager:
with Timer('foo_stuff'):
# do some foo
# do some stuff
Sometimes I find this technique more convenient than timeit - it all depends on what you want to measure.
127k50261337
The absolute best analog of tic and toc would be to simply define them in python.
def tic():
#Homemade version of matlab tic and toc functions
import time
global startTime_for_tictoc
startTime_for_tictoc = time.time()
def toc():
import time
if 'startTime_for_tictoc' in globals():
print "Elapsed time is " + str(time.time() - startTime_for_tictoc) + " seconds."
print "Toc: start time not set"
Then you can use them as:
# do stuff
I had the same question when I migrated to python from Matlab.
With the help of this thread I was able to construct an exact analog of the Matlab tic() and toc() functions.
Simply insert the following code at the top of your script.
import time
def TicTocGenerator():
# Generator that returns time differences
# initial time
tf = time.time() # final time
while True:
tf = time.time()
yield tf-ti # returns the time difference
TicToc = TicTocGenerator() # create an instance of the TicTocGen generator
# This will be the main function through which we define both tic() and toc()
def toc(tempBool=True):
# Prints the time difference yielded by generator instance TicToc
tempTimeInterval = next(TicToc)
if tempBool:
print( "Elapsed time: %f seconds.\n" %tempTimeInterval )
def tic():
# Records a time in TicToc, marks the beginning of a time interval
toc(False)
That's it! Now we are ready to fully use tic() and toc() just as in Matlab.
For example
time.sleep(5)
toc() # returns "Elapsed time: 5.00 seconds."
Actually, this is more versatile than the built-in Matlab functions.
Here, you could create another instance of the TicTocGenerator to keep track of multiple operations, or just to time things differently. For instance, while timing a script, we can now time each piece of the script seperately, as well as the entire script. (I will provide a concrete example)
TicToc2 = TicTocGenerator() # create another instance of the TicTocGen generator
def toc2(tempBool=True):
# Prints the time difference yielded by generator instance TicToc2
tempTimeInterval = next(TicToc2)
if tempBool:
print( "Elapsed time 2: %f seconds.\n" %tempTimeInterval )
def tic2():
# Records a time in TicToc2, marks the beginning of a time interval
toc2(False)
Now you should be able to time two separate things: In the following example, we time the total script and parts of a script separately.
time.sleep(5)
time.sleep(3)
toc2() # returns "Elapsed time 2: 5.00 seconds."
toc() # returns "Elapsed time: 8.00 seconds."
Actually, you do not even need to use tic() each time. If you have a series of commands that you want to time, then you can write
time.sleep(1)
toc() # returns "Elapsed time: 1.00 seconds."
time.sleep(2)
toc() # returns "Elapsed time: 2.00 seconds."
time.sleep(3)
toc() # returns "Elapsed time: 3.00 seconds."
# and so on...
I hope that this is helpful.
Usually, IPython's %time, %timeit, %prun and %lprun (if one has line_profiler installed) satisfy my profiling needs quite well. However, a use case for tic-toc-like functionality arose when I tried to profile calculations that were interactively driven, i.e., by the user's mouse motion in a GUI. I felt like spamming tics and tocs in the sources while testing interactively would be the fastest way to reveal the bottlenecks. I went with Eli Bendersky's Timer class, but wasn't fully happy, since it required me to change the indentation of my code, which can be inconvenient in some editors and confuses the version control system. Moreover, there may be the need to measure the time between points in different functions, which wouldn't work with the with statement. After trying lots of Python cleverness, here is the simple solution that I found worked best:
from time import time
_tstart_stack = []
def tic():
_tstart_stack.append(time())
def toc(fmt="Elapsed: %s s"):
print fmt % (time() - _tstart_stack.pop())
Since this works by pushing the starting times on a stack, it will work correctly for multiple levels of tics and tocs. It also allows one to change the format string of the toc statement to display additional information, which I liked about Eli's Timer class.
For some reason I got concerned with the overhead of a pure Python implementation, so I tested
a C extension module as well:
#include &Python.h&
#include &mach/mach_time.h&
#define MAXDEPTH 100
uint64_t start[MAXDEPTH];
int lvl=0;
static PyObject* tic(PyObject *self, PyObject *args) {
start[lvl++] = mach_absolute_time();
Py_RETURN_NONE;
static PyObject* toc(PyObject *self, PyObject *args) {
return PyFloat_FromDouble(
(double)(mach_absolute_time() - start[--lvl]) / L);
static PyObject* res(PyObject *self, PyObject *args) {
return tic(NULL, NULL), toc(NULL, NULL);
static PyMethodDef methods[] = {
{"tic", tic, METH_NOARGS, "Start timer"},
{"toc", toc, METH_NOARGS, "Stop timer"},
{"res", res, METH_NOARGS, "Test timer resolution"},
{NULL, NULL, 0, NULL}
PyMODINIT_FUNC
inittictoc(void) {
Py_InitModule("tictoc", methods);
This is for MacOSX, and I have omitted code to check if lvl is out of bounds for brevity. While tictoc.res() yields a resolution of about 50 nanoseconds on my system, I found that the jitter of measuring any Python statement is easily in the microsecond range (and much more when used from IPython). At this point, the overhead of the Python implementation becomes negligible, so that it can be used with the same confidence as the C implementation.
I found that the usefulness of the tic-toc-approach is practically limited to code blocks that take more than 10 microseconds to execute. Below that, averaging strategies like in timeit are required to get a faithful measurement.
2,26511325
Have a look at the
It's not really equivalent but if the code you want to time is inside a function you can easily use it.
195k39366480
I changed @Eli Bendersky's answer a little bit to use the ctor __init__() and dtor __del__() to do the timing, so that it can be used more conveniently without indenting the original code:
class Timer(object):
def __init__(self, name=None):
self.name = name
self.tstart = time.time()
def __del__(self):
if self.name:
print '%s elapsed: %.2fs' % (self.name, time.time() - self.tstart)
print 'Elapsed: %.2fs' % (time.time() - self.tstart)
To use, simple put Timer("blahblah") at the beginning of some local scope. Elapsed time will be printed at the end of the scope:
for i in xrange(5):
timer = Timer("eigh()")
x = numpy.random.random(());
x = (x+x.T)/2
numpy.linalg.eigh(x)
It prints out:
eigh() elapsed: 10.13s
eigh() elapsed: 9.74s
eigh() elapsed: 10.70s
eigh() elapsed: 10.25s
eigh() elapsed: 11.28s
This can also be done using a wrapper. Very general way of keeping time.
The wrapper in this example code wraps any function and prints the amount of time needed to execute the function:
def timethis(f):
import time
def wrapped(*args, **kwargs):
start = time.time()
r = f(*args, **kwargs)
print "Executing {0} took {1} seconds".format(f.func_name,
time.time()-start)
return wrapped
def thistakestime():
for x in range():
thistakestime()
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .24947
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 机房下送风优势 的文章

 

随机推荐