Python Timer和TimerFPS计时工具类
目录
Timer用法效果TimerFps用法效果以下工具类代码来自开源项目pyslam
。
Timer
import cv2 class Colors(object): """ Colors class:reset all colors with colors.reset; two sub classes fg for foreground and bg for background; use as colors.subclass.colorname. i.e. colors.fg.red or colors.bg.greenalso, the generic bold, disable, underline, reverse, strike through, and invisible work with the main class i.e. colors.bold """ reset="\033[0m" bold="\033[01m" disable="\033[02m" underline="\033[04m" reverse="\033[07m" strikethrough="\033[09m" invisible="\033[08m" class fg: black="\033[30m" red="\033[31m" green="\033[32m" orange="\033[33m" blue="\033[34m" purple="\033[35m" cyan="\033[36m" lightgrey="\033[37m" darkgrey="\033[90m" lightred="\033[91m" lightgreen="\033[92m" yellow="\033[93m" lightblue="\033[94m" pink="\033[95m" lightcyan="\033[96m" class bg: black="\033[40m" red="\033[41m" green="\033[42m" orange="\033[43m" blue="\033[44m" purple="\033[45m" cyan="\033[46m" lightgrey="\033[47m" class Printer(object): @staticmethod def red(*args, **kwargs): print(Colors.fg.red, *args, **kwargs) print(Colors.reset, end="") @staticmethod def green(*args, **kwargs): print(Colors.fg.green, *args, **kwargs) print(Colors.reset, end="") @staticmethod def blue(*args, **kwargs): print(Colors.fg.blue, *args, **kwargs) print(Colors.reset, end="") @staticmethod def cyan(*args, **kwargs): print(Colors.fg.cyan, *args, **kwargs) print(Colors.reset, end="") @staticmethod def orange(*args, **kwargs): print(Colors.fg.orange, *args, **kwargs) print(Colors.reset, end="") @staticmethod def purple(*args, **kwargs): print(Colors.fg.purple, *args, **kwargs) print(Colors.reset, end="") @staticmethod def yellow(*args, **kwargs): print(Colors.fg.yellow, *args, **kwargs) print(Colors.reset, end="") @staticmethod def error(*args, **kwargs): print(Colors.fg.red, *args, **kwargs, file=sys.stderr) print(Colors.reset, end="") #timer_print = print timer_print = Printer.cyan class Timer: def __init__(self, name = "", is_verbose = False): self._name = name self._is_verbose = is_verbose self._is_paused = False self._start_time = None self._accumulated = 0 self._elapsed = 0 self.start() def start(self): self._accumulated = 0 self._start_time = cv2.getTickCount() def pause(self): now_time = cv2.getTickCount() self._accumulated += (now_time - self._start_time)/cv2.getTickFrequency() self._is_paused = True def resume(self): if self._is_paused: # considered only if paused self._start_time = cv2.getTickCount() self._is_paused = False def elapsed(self): if self._is_paused: self._elapsed = self._accumulated else: now = cv2.getTickCount() self._elapsed = self._accumulated + (now - self._start_time)/cv2.getTickFrequency() if self._is_verbose is True: name = self._name if self._is_paused: name += " [paused]" message = "Timer::" + name + " - elapsed: " + str(self._elapsed) timer_print(message) return self._elapsed
用法
import Timer timer = Timer(is_verbose=True) timer.start() # 此处是你的代码 timer.elapsed()
效果
TimerFps
在上面的基础上,再加上下面代码段:
import os import numpy as np import cv2 import math class MovingAverage: def __init__(self, average_width = 10, compute_sigma = False): self._average_width = average_width self._idx_ring = 0 self._average = 0 self._sigma2 = 0 self._is_init = False self._is_compute_sigma = compute_sigma self._one_over_average_width_min_one = 1./(average_width-1) self._ring_buffer = np.zeros(average_width) def init(self, initVal=None): if initVal is None: initVal = 0. self._ring_buffer = np.full(self._average_width, initVal, dtype=np.float32) self._average = initVal; self._sigma2 = 0; self._is_init = True; def getAverage(self, new_val=None): if not self._is_init: self.init(new_val) if new_val is None: return self._average averageOld = self._average oldVal = self._ring_buffer[self._idx_ring] self._average += (new_val - oldVal)/self._average_width if self._is_compute_sigma: self._sigma2 = self._sigma2 + self._one_over_average_width_min_one*(self._average_width*(averageOld*averageOld - self._average*self._average) - oldVal*oldVal + newVal*newVal) self._ring_buffer[self._idx_ring] = new_val self._idx_ring = (self._idx_ring + 1) % self._average_width return self._average def getSigma(self): return math.sqrt(max(self._sigma2,0.)) class TimerFps(Timer): def __init__(self, name="", average_width = 10, is_verbose = True): super().__init__(name, is_verbose) self.moving_average = MovingAverage(average_width) def refresh(self): elapsed = self.elapsed() self.moving_average.getAverage(elapsed) self.start() if self._is_verbose is True: dT = self.moving_average.getAverage() name = self._name if self._is_paused: name += " [paused]" message = "Timer::" + name + " - fps: " + str(1./dT) + ", T: " + str(dT) timer_print(message)
用法
import TimerFps timer = TimerFps(name="") timer.start() # 这里是你的代码 timer.refresh()
效果
到此这篇关于Python Timer和TimerFPS计时工具类的文章就介绍到这了,更多相关Python 计时工具类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?