Day 5

A Basic Timer Function in Python

Try it!

You may need to wait or refresh to let it load



Description


This is a program that calculates factorials using recursion [a new style I have been learning about] and pyscript.

View the source code


import time
def timer(func):
 def wrapper(*args, **kwargs):
  start = time.time()
  result = func(*args,**kwargs)
  end = time.time()
  print(f"{func.__name__} took {end-start} seconds")
  return result
 return wrapper
@timer
def factorial(num):
 if num == 0:
  return 0
 elif num == 1:
  return 1
 return factorial(num-1)+factorial(num-2)
print(factorial(3))

Previous Day
Next Day