import time class Timer object def __enter__ self self _startTime time

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import time
class Timer(object):
def __enter__(self):
self._startTime = time.time()
def __exit__(self, exc_type, exc_val, exc_tb):
print ("Elapsed time: {:.9f} sec".format(time.time() - self._startTime))
# Замерять время так:
with Timer():
a = 1
b = a+2