-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresources.py
More file actions
45 lines (39 loc) · 1.11 KB
/
resources.py
File metadata and controls
45 lines (39 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
A timing utility class inspired in Fast Downward's equivalent.
See http://www.fast-downward.org/
"""
import contextlib
import os
import sys
import time
import psutil
class Timer:
def __init__(self):
self.start_time = time.time()
self.start_clock = self._clock()
self.start_mem = psutil.Process().memory_info().rss
@staticmethod
def _clock():
times = os.times()
return times[0] + times[1]
def __str__(self):
current = psutil.Process().memory_info().rss
current_in_mb = current / (1024*1024)
rss_in_mb = (current - self.start_mem) / (1024*1024)
return "[%.2fs CPU, %.2fs wall-clock, diff: %.2fMB, curr: %.2fMB]" % (
self._clock() - self.start_clock,
time.time() - self.start_time, rss_in_mb, current_in_mb)
@contextlib.contextmanager
def timing(text, newline=False):
timer = Timer()
if newline:
print(f"{text}...")
else:
print(f"{text}...", end=' ')
sys.stdout.flush()
yield
if newline:
print(f"{text}: {timer}")
else:
print(timer)
sys.stdout.flush()