当前位置: 移动技术网 > IT编程>脚本编程>Python > Python 实现一个计时器

Python 实现一个计时器

2020年07月29日  | 移动技术网IT编程  | 我要评论

问题

你想记录程序执行多个任务所花费的时间

解决方案

time 模块包含很多函数来执行跟时间有关的函数。 尽管如此,通常我们会在此基础之上构造一个更高级的接口来模拟一个计时器。例如:

import time

class timer:
  def __init__(self, func=time.perf_counter):
    self.elapsed = 0.0
    self._func = func
    self._start = none

  def start(self):
    if self._start is not none:
      raise runtimeerror('already started')
    self._start = self._func()

  def stop(self):
    if self._start is none:
      raise runtimeerror('not started')
    end = self._func()
    self.elapsed += end - self._start
    self._start = none

  def reset(self):
    self.elapsed = 0.0

  @property
  def running(self):
    return self._start is not none

  def __enter__(self):
    self.start()
    return self

  def __exit__(self, *args):
    self.stop()

这个类定义了一个可以被用户根据需要启动、停止和重置的计时器。 它会在 elapsed 属性中记录整个消耗时间。 下面是一个例子来演示怎样使用它:

def countdown(n):
  while n > 0:
    n -= 1

# use 1: explicit start/stop
t = timer()
t.start()
countdown(1000000)
t.stop()
print(t.elapsed)

# use 2: as a context manager
with t:
  countdown(1000000)

print(t.elapsed)

with timer() as t2:
  countdown(1000000)
print(t2.elapsed)

讨论

本节提供了一个简单而实用的类来实现时间记录以及耗时计算。 同时也是对使用with语句以及上下文管理器协议的一个很好的演示。

在计时中要考虑一个底层的时间函数问题。一般来说, 使用 time.time() time.clock() 计算的时间精度因操作系统的不同会有所不同。 而使用 time.perf_counter() 函数可以确保使用系统上面最精确的计时器。

上述代码中由 timer 类记录的时间是钟表时间,并包含了所有休眠时间。 如果你只想计算该进程所花费的cpu时间,应该使用 time.process_time() 来代替:

t = timer(time.process_time)
with t:
  countdown(1000000)
print(t.elapsed)

time.perf_counter() time.process_time() 都会返回小数形式的秒数时间。 实际的时间值没有任何意义,为了得到有意义的结果,你得执行两次函数然后计算它们的差值。

以上就是python 实现一个计时器的详细内容,更多关于python 计时器的资料请关注移动技术网其它相关文章!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网