diff --git a/task2.py b/task2.py new file mode 100644 index 0000000..a2c4716 --- /dev/null +++ b/task2.py @@ -0,0 +1,31 @@ +import time + +'''Вспомогательная функция для тестов''' +def Sum(m): + s = 0 + for i in range(0, m): + s += i + return s + +'''Менеджер контекста''' +class Timer(): + def __init__(self): + self.start = None + + def __enter__(self): + self.start = time.time() + return self + + def __exit__(self, exc_type, exc_value, exc_traceback): + print(time.time() - self.start) + +'''Проверка Менеджера контекста''' +with Timer(): + with Timer(): + print(Sum(1000)) + + with Timer(): + print('sum: ' + str(Sum(10000000)), end=', time: ') + + with Timer(): + time.sleep(1.4)