Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python timer decorator

import time
import functools
def time_it(func):
  @functools.wraps(func)
  def wrapper(*args, **kwargs):
    start = time.perf_counter() # Start Time
    result = func(*args, **kwargs)
    end = time.perf_counter() # End Time
	print(f"function {func.__name__} ran in {end - start:0.4f} seconds")
  	return result
  return wrapper

@time_it
def some_function(sec: int):
  time.sleep(sec)

if __name__ == "__main__":
  some_function(5)
Source by dev.to #
 
PREVIOUS NEXT
Tagged: #python #timer #decorator
ADD COMMENT
Topic
Name
9+3 =