Coverage for kwave/utils/tictoc.py: 50%

12 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2025-03-24 12:06 -0700

1from time import perf_counter 

2 

3 

4class TicToc(object): 

5 """ 

6 A class for measuring the execution time of a code block. 

7 

8 This class uses the perf_counter function from the time module to measure the 

9 execution time of a code block. It provides a simple interface with two methods: 

10 tic and toc. You can use the tic method to start the timer, and then use the 

11 toc method to stop the timer and get the elapsed time. 

12 """ 

13 

14 start_time = -1 

15 

16 @staticmethod 

17 def tic(): 

18 """ 

19 Start the timer. 

20 

21 This method sets the start_time attribute to the current time, as measured 

22 by the perf_counter function from the time module. 

23 """ 

24 TicToc.start_time = perf_counter() 

25 

26 @staticmethod 

27 def toc(reset: bool = False) -> float: 

28 """Stop the timer and return the elapsed time. 

29 

30 This method calculates the elapsed time since the timer was started by 

31 subtracting the start_time attribute from the current time, as measured by 

32 the perf_counter function from the time module. If the reset argument is 

33 True, the timer will be restarted automatically. 

34 

35 Args: 

36 reset: Whether to reset the timer after stopping it. 

37 

38 Returns: 

39 The elapsed time in seconds. 

40 """ 

41 passed_time = perf_counter() - TicToc.start_time 

42 if reset: 

43 TicToc.tic() 

44 return passed_time