File size: 973 Bytes
0b32ad6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
import inspect
from collections import defaultdict
import torch


class Timer():
    def __init__(self):
        self.timings = {}
        self.start_time = 0

    def start(self):
        self.start_time = time.time()
    

    def end(self):
        frameinfo = inspect.getouterframes( inspect.currentframe() )[1]
        filename = frameinfo.filename
        filename = '/'.join(filename.split('/')[-2:])
        marker = f'{filename}:{frameinfo.lineno}'
        if marker not in self.timings.keys():
            self.timings[marker] = []
        else:
            self.timings[marker].append( float(time.time() - self.start_time) )

    def report(self):
        n_points = len(self.timings.keys())
        if n_points > 0:
            print('[TIMER]:')
            for marker in self.timings:
                print(f'{marker}: {torch.FloatTensor(self.timings[marker]).mean().item()}')
        else:
            print('[TIMER]: No record')

timer = Timer()