Skip to content

Instantly share code, notes, and snippets.

@keuv-grvl
Last active April 28, 2023 06:50
Show Gist options
  • Save keuv-grvl/58b52e919877e624337b8ee64a2f2dde to your computer and use it in GitHub Desktop.
Save keuv-grvl/58b52e919877e624337b8ee64a2f2dde to your computer and use it in GitHub Desktop.
Profile a fonction with cProfile
@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
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