context_3.py 657 B

12345678910111213141516171819202122232425262728293031323334
  1. import time
  2. def calculate():
  3. for i in range(10000):
  4. 2 ** 2123
  5. # Расчет время выполнения функций
  6. class Timer:
  7. def __enter__(self):
  8. self.start = time.time()
  9. self.end = 0.0
  10. return lambda: self.end - self.start
  11. def __exit__(self, *args):
  12. self.end = time.time()
  13. def main():
  14. with Timer() as my_timer:
  15. time.sleep(0.6)
  16. print(my_timer())
  17. with Timer() as my_timer2:
  18. time.sleep(1.5)
  19. print(my_timer2())
  20. with Timer() as my_timer3:
  21. calculate()
  22. print(my_timer3())
  23. if __name__ == '__main__':
  24. main()