Guest User

Untitled

a guest
Jul 25th, 2020
47
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import pstats
  3. import time
  4. from cProfile import Profile
  5.  
  6.  
  7. """
  8. Use it like this:
  9.  
  10. @profile_it('C:/my/profile/directory')
  11. def some_function(a, b, c):
  12.    ...some logic...
  13. """
  14.  
  15. def profile_it(save_dir):
  16.     def decorator(method):
  17.         def inner(*args, **kwargs):
  18.             profiler = Profile()
  19.             file_name = os.path.join(save_dir, f'{method.__name__}_{int(time.time() * 1000)}.prof')
  20.  
  21.             result = None
  22.  
  23.             try:
  24.                 result = profiler.runcall(method, *args, **kwargs)
  25.             except Exception as e:
  26.                 print(f'Could not run method call: {e}')
  27.             finally:
  28.                 stats = pstats.Stats(profiler)
  29.                 stats.dump_stats(file_name)
  30.             return result
  31.         return inner
  32.     return decorator
RAW Paste Data