Last active
June 24, 2026 14:08
-
-
Save fherbine/f9f9edfd700959a14c78820f035cc77f to your computer and use it in GitHub Desktop.
Profiling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cProfile | |
| import threading | |
| def _run_profiler(run): | |
| """Use as a decorator for Thread profiling ONLY""" | |
| def wrapper(self): | |
| profiler = cProfile.Profile() | |
| profiler.enable() | |
| run(self) | |
| profiler.disable() | |
| try: | |
| return profiler.runcall(threading.Thread.run, self) | |
| finally: | |
| profiler.dump_stats('myprofile-%d.profile' % (self.ident,)) | |
| return wrapper | |
| from functools import wraps | |
| def profile(output_file="profile.prof"): | |
| def decorator(func): | |
| @wraps(func) | |
| def wrapper(*args, **kwargs): | |
| profiler = cProfile.Profile() | |
| profiler.enable() | |
| result = func(*args, **kwargs) | |
| profiler.disable() | |
| profiler.dump_stats(output_file) | |
| return result | |
| return wrapper | |
| return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment