nanopyx.core.utils.timeit
1import time 2import types 3 4 5def timeit(func: types.FunctionType): 6 """ 7 Decorator to measure the execution time of a function and print the result in seconds. 8 9 Args: 10 func (types.FunctionType): The function to be decorated. 11 12 Returns: 13 Callable: The decorated function. 14 15 Example: 16 @timeit 17 def my_function(): 18 # Your code here 19 20 When my_function is called, it will print the execution time in seconds. 21 22 """ 23 24 def wrapper(*args, **kwargs): 25 t = time.time() 26 retval = func(*args, **kwargs) 27 print(f"{func.__name__} took {round(time.time()-t,3)} seconds") 28 return retval 29 30 return wrapper 31 32 33def timeit2(func): 34 """ 35 Decorator to measure the execution time of a function and print the result in seconds, milliseconds, or nanoseconds. 36 37 Args: 38 func (Callable): The function to be decorated. 39 40 Returns: 41 Callable: The decorated function. 42 43 Example: 44 @timeit2 45 def my_function(): 46 # Your code here 47 48 When my_function is called, it will print the execution time in an appropriate time unit (s, ms, or ns). 49 50 """ 51 52 def wrapper(*args, **kwargs): 53 start = time.time() 54 result = func(*args, **kwargs) 55 end = time.time() 56 delta = end - start 57 if delta < 10**-4: 58 msg = f"{func.__name__} took {delta/1e-6:.6f} nseconds" 59 elif delta < 10**-1: 60 msg = f"{func.__name__} took {delta/1e-3:.6f} mseconds" 61 else: 62 msg = f"{func.__name__} took {delta:.6f} seconds" 63 print(msg) 64 return result 65 66 return wrapper
def
timeit(func: function):
6def timeit(func: types.FunctionType): 7 """ 8 Decorator to measure the execution time of a function and print the result in seconds. 9 10 Args: 11 func (types.FunctionType): The function to be decorated. 12 13 Returns: 14 Callable: The decorated function. 15 16 Example: 17 @timeit 18 def my_function(): 19 # Your code here 20 21 When my_function is called, it will print the execution time in seconds. 22 23 """ 24 25 def wrapper(*args, **kwargs): 26 t = time.time() 27 retval = func(*args, **kwargs) 28 print(f"{func.__name__} took {round(time.time()-t,3)} seconds") 29 return retval 30 31 return wrapper
Decorator to measure the execution time of a function and print the result in seconds.
Args: func (types.FunctionType): The function to be decorated.
Returns: Callable: The decorated function.
Example: @timeit def my_function(): # Your code here
When my_function is called, it will print the execution time in seconds.
def
timeit2(func):
34def timeit2(func): 35 """ 36 Decorator to measure the execution time of a function and print the result in seconds, milliseconds, or nanoseconds. 37 38 Args: 39 func (Callable): The function to be decorated. 40 41 Returns: 42 Callable: The decorated function. 43 44 Example: 45 @timeit2 46 def my_function(): 47 # Your code here 48 49 When my_function is called, it will print the execution time in an appropriate time unit (s, ms, or ns). 50 51 """ 52 53 def wrapper(*args, **kwargs): 54 start = time.time() 55 result = func(*args, **kwargs) 56 end = time.time() 57 delta = end - start 58 if delta < 10**-4: 59 msg = f"{func.__name__} took {delta/1e-6:.6f} nseconds" 60 elif delta < 10**-1: 61 msg = f"{func.__name__} took {delta/1e-3:.6f} mseconds" 62 else: 63 msg = f"{func.__name__} took {delta:.6f} seconds" 64 print(msg) 65 return result 66 67 return wrapper
Decorator to measure the execution time of a function and print the result in seconds, milliseconds, or nanoseconds.
Args: func (Callable): The function to be decorated.
Returns: Callable: The decorated function.
Example: @timeit2 def my_function(): # Your code here
When my_function is called, it will print the execution time in an appropriate time unit (s, ms, or ns).