日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python3 打点计时

發(fā)布時(shí)間:2023/12/18 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 打点计时 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用方法:?

(1)記錄時(shí)間點(diǎn):? ? 打點(diǎn)()?

(2)計(jì)算時(shí)間差值: 計(jì)時(shí)()

?

示例:

if __name__ == '__main__':打點(diǎn)()time.sleep(1)打點(diǎn)("a2")time.sleep(1)打點(diǎn)("a3")time.sleep(2)打點(diǎn)("a4")time.sleep(3)打點(diǎn)()print(f"\n默認(rèn)情況取最后的兩點(diǎn):\n"f"{計(jì)時(shí)()}")print(f"\n下標(biāo)的使用跟數(shù)組截取一樣:\n"f"{計(jì)時(shí)(0, 3)}")print(計(jì)時(shí)(0))print(f"\n也可以通過(guò)打點(diǎn)時(shí)定義的名稱來(lái)截取范圍:\n"f"{計(jì)時(shí)('a2', 'a4')}")print()print()print(計(jì)時(shí)().秒數(shù)())print(計(jì)時(shí)().可視化時(shí)間())print(計(jì)時(shí)().最近計(jì)算的可視化時(shí)間字典())

?

?

?

?

工具類:

import time# region 打點(diǎn)計(jì)時(shí)# region 轉(zhuǎn)換秒數(shù)相關(guān) _毫秒_秒數(shù) = 0.001 _秒_秒數(shù) = _毫秒_秒數(shù) * 1000 _分鐘_秒數(shù) = _秒_秒數(shù) * 60 _小時(shí)_秒數(shù) = _分鐘_秒數(shù) * 60 _天_秒數(shù) = _小時(shí)_秒數(shù) * 24def 拆解秒數(shù)(秒數(shù), 各時(shí)間單位值字典={}):if 秒數(shù) > _天_秒數(shù) + _小時(shí)_秒數(shù):除余結(jié)果 = divmod(秒數(shù), _天_秒數(shù))各時(shí)間單位值字典["天"] = int(除余結(jié)果[0])return "%d 天, %s" % (int(除余結(jié)果[0]), 拆解秒數(shù)(除余結(jié)果[1], 各時(shí)間單位值字典))elif 秒數(shù) > _小時(shí)_秒數(shù) + _分鐘_秒數(shù):除余結(jié)果 = divmod(秒數(shù), _小時(shí)_秒數(shù))各時(shí)間單位值字典["小時(shí)"] = int(除余結(jié)果[0])return '%d 小時(shí), %s' % (int(除余結(jié)果[0]), 拆解秒數(shù)(除余結(jié)果[1], 各時(shí)間單位值字典))elif 秒數(shù) > _分鐘_秒數(shù) + _秒_秒數(shù):除余結(jié)果 = divmod(秒數(shù), _分鐘_秒數(shù))各時(shí)間單位值字典["分鐘"] = int(除余結(jié)果[0])return '%d 分鐘, %s' % (int(除余結(jié)果[0]), 拆解秒數(shù)(除余結(jié)果[1], 各時(shí)間單位值字典))elif 秒數(shù) > _秒_秒數(shù) + _毫秒_秒數(shù):除余結(jié)果 = divmod(秒數(shù), _秒_秒數(shù))各時(shí)間單位值字典["秒"] = int(除余結(jié)果[0])return '%d 秒, %s' % (int(除余結(jié)果[0]), 拆解秒數(shù)(除余結(jié)果[1], 各時(shí)間單位值字典))else:除余結(jié)果 = divmod(秒數(shù), _毫秒_秒數(shù))各時(shí)間單位值字典["毫秒"] = int(除余結(jié)果[0])return "%d 毫秒" % int(除余結(jié)果[0])# endregionclass 打點(diǎn)計(jì)時(shí)類:class 時(shí)間值存儲(chǔ)類:def __init__(self, 時(shí)間值=0):self.時(shí)間值 = 時(shí)間值self.各時(shí)間單位值字典 = {}@staticmethoddef 實(shí)例化():return 打點(diǎn)計(jì)時(shí)類.時(shí)間值存儲(chǔ)類()def 可視化時(shí)間(self):# 清空字典self.各時(shí)間單位值字典["天"] = 0self.各時(shí)間單位值字典["小時(shí)"] = 0self.各時(shí)間單位值字典["分鐘"] = 0self.各時(shí)間單位值字典["秒"] = 0self.各時(shí)間單位值字典["毫秒"] = 0return 拆解秒數(shù)(self.時(shí)間值, self.各時(shí)間單位值字典)def 秒數(shù)(self):return self.時(shí)間值def 最近計(jì)算的可視化時(shí)間字典(self):return self.各時(shí)間單位值字典def __str__(self):return self.可視化時(shí)間()def __init__(self, 數(shù)組型打點(diǎn)上限=150, 刪除區(qū)間=[5, -5]):self.默認(rèn)打點(diǎn)數(shù)組 = []self.個(gè)性打點(diǎn)字典 = {}self.時(shí)間值存儲(chǔ)實(shí)例 = 打點(diǎn)計(jì)時(shí)類.時(shí)間值存儲(chǔ)類.實(shí)例化()self.數(shù)組型打點(diǎn)上限 = 數(shù)組型打點(diǎn)上限self.刪除區(qū)間 = 刪除區(qū)間@staticmethoddef 實(shí)例化(數(shù)組型打點(diǎn)上限=150, 刪除區(qū)間=[5, -5]):return 打點(diǎn)計(jì)時(shí)類(數(shù)組型打點(diǎn)上限, 刪除區(qū)間)def 打點(diǎn)(self, 計(jì)時(shí)點(diǎn)名稱=None):計(jì)時(shí)點(diǎn) = time.time()if len(self.默認(rèn)打點(diǎn)數(shù)組) > self.數(shù)組型打點(diǎn)上限:self.默認(rèn)打點(diǎn)數(shù)組 = self.默認(rèn)打點(diǎn)數(shù)組[0:self.刪除區(qū)間[0]] + self.默認(rèn)打點(diǎn)數(shù)組[self.刪除區(qū)間[1]:]self.默認(rèn)打點(diǎn)數(shù)組.append(計(jì)時(shí)點(diǎn))if 計(jì)時(shí)點(diǎn)名稱:計(jì)時(shí)點(diǎn)名稱 = str(計(jì)時(shí)點(diǎn)名稱)self.個(gè)性打點(diǎn)字典[計(jì)時(shí)點(diǎn)名稱] = 計(jì)時(shí)點(diǎn)def 計(jì)時(shí)(self, 起始點(diǎn)=None, 結(jié)束點(diǎn)=None):def 無(wú)參_無(wú)參_處理():所需計(jì)時(shí)點(diǎn)_數(shù)組 = self.默認(rèn)打點(diǎn)數(shù)組[-2:]時(shí)間差值 = 所需計(jì)時(shí)點(diǎn)_數(shù)組[0] - 所需計(jì)時(shí)點(diǎn)_數(shù)組[-1]if 時(shí)間差值 < 0:時(shí)間差值 = -時(shí)間差值self.時(shí)間值存儲(chǔ)實(shí)例.時(shí)間值 = 時(shí)間差值return self.時(shí)間值存儲(chǔ)實(shí)例def 下標(biāo)_下標(biāo)_處理():所需計(jì)時(shí)點(diǎn)_數(shù)組 = self.默認(rèn)打點(diǎn)數(shù)組[起始點(diǎn):結(jié)束點(diǎn)]時(shí)間差值 = 所需計(jì)時(shí)點(diǎn)_數(shù)組[0] - 所需計(jì)時(shí)點(diǎn)_數(shù)組[-1]if 時(shí)間差值 < 0:時(shí)間差值 = -時(shí)間差值self.時(shí)間值存儲(chǔ)實(shí)例.時(shí)間值 = 時(shí)間差值return self.時(shí)間值存儲(chǔ)實(shí)例def 下標(biāo)_無(wú)參_處理():所需計(jì)時(shí)點(diǎn)_數(shù)組 = self.默認(rèn)打點(diǎn)數(shù)組[起始點(diǎn):]時(shí)間差值 = 所需計(jì)時(shí)點(diǎn)_數(shù)組[0] - 所需計(jì)時(shí)點(diǎn)_數(shù)組[-1]if 時(shí)間差值 < 0:時(shí)間差值 = -時(shí)間差值self.時(shí)間值存儲(chǔ)實(shí)例.時(shí)間值 = 時(shí)間差值return self.時(shí)間值存儲(chǔ)實(shí)例def 名稱_名稱_處理():時(shí)間差值 = self.個(gè)性打點(diǎn)字典[起始點(diǎn)] - self.個(gè)性打點(diǎn)字典[結(jié)束點(diǎn)]if 時(shí)間差值 < 0:時(shí)間差值 = -時(shí)間差值self.時(shí)間值存儲(chǔ)實(shí)例.時(shí)間值 = 時(shí)間差值return self.時(shí)間值存儲(chǔ)實(shí)例def default():raise Exception("計(jì)時(shí)失敗,參數(shù)類型未支持")switch = {"<class 'NoneType'><class 'NoneType'>": 無(wú)參_無(wú)參_處理,"<class 'int'><class 'int'>": 下標(biāo)_下標(biāo)_處理,"<class 'int'><class 'NoneType'>": 下標(biāo)_無(wú)參_處理,"<class 'str'><class 'str'>": 名稱_名稱_處理}try:return switch.get(repr(type(起始點(diǎn))) + repr(type(結(jié)束點(diǎn))), default)()except Exception as e:print("計(jì)時(shí)出錯(cuò)")print(e)_靜態(tài)計(jì)時(shí)器 = 打點(diǎn)計(jì)時(shí)類.實(shí)例化()def 打點(diǎn)(計(jì)時(shí)點(diǎn)名稱=None):_靜態(tài)計(jì)時(shí)器.打點(diǎn)(計(jì)時(shí)點(diǎn)名稱)def 計(jì)時(shí)(起始點(diǎn)=None, 結(jié)束點(diǎn)=None):return _靜態(tài)計(jì)時(shí)器.計(jì)時(shí)(起始點(diǎn), 結(jié)束點(diǎn))# endregion 打點(diǎn)計(jì)時(shí)

?

?

總結(jié)

以上是生活随笔為你收集整理的python3 打点计时的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。