Last active
April 28, 2023 06:50
-
-
Save keuv-grvl/58b52e919877e624337b8ee64a2f2dde to your computer and use it in GitHub Desktop.
Profile a fonction with cProfile
This file contains 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
@profile | |
def func1(i: float): | |
import time | |
time.sleep(i) | |
@profile | |
def func_mother(): | |
func1(1.2) | |
a = 2 + 3 | |
import numpy as np | |
return np.ones(5) - 1.2 | |
if __name__ == "__main__": | |
for i in range(20): | |
func1(i / 11) | |
r = func_mother() | |
print(r.shape) | |
# run with | |
# $ python -m kernprof -l -v -u 0.001 profile_with_kernprof.py [args] | |
# $ python -m line_profiler profile_with_kernprof.py.lprof |
This file contains 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 | |
def profileit(name): | |
def inner(func): | |
def wrapper(*args, **kwargs): | |
prof = cProfile.Profile() | |
retval = prof.runcall(func, *args, **kwargs) | |
# Note use of name from outer scope | |
prof.dump_stats(name) | |
return retval | |
return wrapper | |
return inner | |
@profileit("profile_for_func1_001") | |
def func1(i: float): | |
import time | |
time.sleep(i) | |
@profileit("profile_for_func1_001") | |
def func_mother(): | |
func1(1.2) | |
a = 2 + 3 | |
import numpy as np | |
return np.ones(5) - 1.2 | |
if __name__ == "__main__": | |
for i in range(20): | |
func1(i / 11) | |
r = func_mother() | |
print(r.shape) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment