Matlab的tic和toc函数的Python等效项是什么? [英] What is the Python equivalent of Matlab's tic and toc functions?
本文介绍了Matlab的tic和toc函数的Python等效项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Matlab的 tic和toc函数的Python等效语言是什么?
What is the Python equivalent of Matlab's tic and toc functions?
推荐答案
除了ThiefMaster提到的timeit
,一种简单的方法就是(导入time
之后):
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
有时候,我发现此技术比timeit
更方便-这完全取决于您要测量的内容.
Sometimes I find this technique more convenient than timeit
- it all depends on what you want to measure.
这篇关于Matlab的tic和toc函数的Python等效项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文