Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import pstats
- import time
- from cProfile import Profile
- """
- Use it like this:
- @profile_it('C:/my/profile/directory')
- def some_function(a, b, c):
- ...some logic...
- """
- def profile_it(save_dir):
- def decorator(method):
- def inner(*args, **kwargs):
- profiler = Profile()
- file_name = os.path.join(save_dir, f'{method.__name__}_{int(time.time() * 1000)}.prof')
- result = None
- try:
- result = profiler.runcall(method, *args, **kwargs)
- except Exception as e:
- print(f'Could not run method call: {e}')
- finally:
- stats = pstats.Stats(profiler)
- stats.dump_stats(file_name)
- return result
- return inner
- return decorator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement