Skip to content

Commit 912793e

Browse files
committed
debug Timeit
1 parent b509090 commit 912793e

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ torch4keras.egg-info/
66
build/
77
dist/
88
backup
9-
test
109
.DS_Store
1110
*.pt
1211
*.log

test/test_log.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from torch4keras.snippets import LoggerHandler
2+
import time
3+
log = LoggerHandler(log_path='./log/log.log', handles=['StreamHandler', 'TimedRotatingFileHandler'],
4+
handle_config={'when': 'S', 'interval': 3, 'backupCount': 2, 'encoding': 'utf-8'})
5+
6+
log.debug('debug')
7+
log.info('info')
8+
log.warning('warning')
9+
log.error('error')
10+
log.critical('critical')
11+
12+
for i in range(10):
13+
log.info('hello '+str(i))
14+
time.sleep(2)

test/test_time.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from torch4keras.snippets import timeit, Timeit
2+
import time
3+
4+
5+
# 装饰器
6+
@timeit
7+
def func(n=10):
8+
for _ in range(n):
9+
time.sleep(0.1)
10+
func()
11+
12+
# 上下文管理器 - 统计累计耗时
13+
with Timeit() as ti:
14+
for i in range(10):
15+
time.sleep(0.1)
16+
ti.lap(prefix=i, restart=False) # 统计累计耗时
17+
18+
# 上下文管理器 - 统计每段速度
19+
with Timeit() as ti:
20+
for i in range(10):
21+
time.sleep(0.1)
22+
ti.lap(count=10, prefix=i, restart=True)
23+
ti(10) # 统计速度
24+
25+
26+
# 上下文管理器 - 统计速度
27+
with Timeit() as ti:
28+
for i in range(10):
29+
time.sleep(0.1)
30+
ti.lap(prefix=i, restart=True)
31+
ti(10) # 统计速度

torch4keras/snippets/monitor.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,65 @@ def warpper(*args, **kwargs):
5252
return warpper
5353

5454

55+
class Timeit:
56+
'''上下文管理器, 记录耗时/平均耗时
57+
58+
Example
59+
----------------------
60+
from torch4keras.snippets import Timeit
61+
with Timeit() as ti:
62+
for i in range(10):
63+
time.sleep(0.1)
64+
# ti.lap(prefix=i, restart=False) # 统计累计耗时
65+
# ti.lap(prefix=i, restart=True) # 统计间隔耗时
66+
# ti.lap(count=10, prefix=i, restart=True) # 统计每段速度
67+
# ti(10) # 统计速度
68+
'''
69+
def __enter__(self):
70+
self.count = None
71+
self.start_tm = time.time()
72+
self.template = 'Average speed: {:.2f}/s'
73+
return self
74+
75+
def __call__(self, count):
76+
self.count = count
77+
78+
def restart(self):
79+
'''自定义开始记录的地方'''
80+
self.start_tm = time.time()
81+
82+
def lap(self, count:int=None, prefix:str=None, restart=False):
83+
'''
84+
:params count: 需要计算平均生成速度中统计的次数
85+
:params prefix: 打印时候自定义的前缀
86+
:params restart: 是否重置start_tm, True只记录时间间隔,否则记录的是从一开始的累计时间
87+
'''
88+
if count is not None:
89+
self.count = count
90+
prefix = '' if prefix is None else str(prefix).strip() + ' - '
91+
92+
end_tm = time.time()
93+
consume = end_tm - self.start_tm
94+
if self.count is None:
95+
consume = format_time(consume, hhmmss=False)
96+
start1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.start_tm))
97+
end1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(end_tm))
98+
log_info(prefix + f'Cost {consume} [{start1} < {end1}]')
99+
elif consume > 0:
100+
speed = self.count / consume
101+
log_info(prefix + self.template.format(speed))
102+
else:
103+
pass
104+
# log_warn('Time duration = 0')
105+
106+
if restart:
107+
self.restart()
108+
109+
def __exit__(self, exc_type, exc_val, exc_tb):
110+
self.lap()
111+
print()
112+
113+
55114
def send_email(mail_receivers:Union[str,list], mail_subject:str, mail_msg:str="", mail_host:str=None,
56115
mail_user:str=None, mail_pwd:str=None, mail_sender:str=None):
57116
''' 发送邮件(默认使用笔者自己注册的邮箱,若含敏感信息请使用自己注册的邮箱)

0 commit comments

Comments
 (0)