SHARE
TWEET

Untitled

a guest Apr 15th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import functools
  2. import time
  3.  
  4. def timer(func):
  5.     """Print the runtime of the decorated function"""
  6.     @functools.wraps(func)
  7.     def wrapper_timer(*args, **kwargs):
  8.         start_time = time.perf_counter()    # 1
  9.         value = func(*args, **kwargs)
  10.         end_time = time.perf_counter()      # 2
  11.         run_time = end_time - start_time    # 3
  12.         print(f"Finished {func.__name__!r} in {run_time:.4f} secs")
  13.         return value
  14.     return wrapper_timer
  15.  
  16. @timer
  17. def demo(num_times):
  18.     for _ in range(num_times):
  19.         sum([i**2 for i in range(10000)])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top