Skip to content

Instantly share code, notes, and snippets.

@danriti
Last active February 26, 2022 07:20
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danriti/8015889 to your computer and use it in GitHub Desktop.
Save danriti/8015889 to your computer and use it in GitHub Desktop.
Line Profiling in Python
#!/usr/bin/python
from time import sleep
def so_slow(bar):
""" Simulate a slow function. """
sleep(5)
return bar
@profile
def its_time_for_the_calculator(foo):
""" It's time for the calculator. """
if not isinstance(foo, int):
return None
a = []
for i in xrange(foo):
a.append(i)
b = so_slow(a)
c = 0
for i in xrange(foo):
c += i
return None
def main():
print its_time_for_the_calculator(100000)
if __name__ == "__main__":
main()
@ckarany
Copy link

ckarany commented Feb 11, 2015

Where is "@Profile" decorator defined?

@neara
Copy link

neara commented Jul 13, 2015

exactly

@stringhamc
Copy link

You have to call kernprof with the -l flag for the @Profile decorator to be recognized

@leannaking
Copy link

What is kernprof.py?

@jerilkuriakose
Copy link

In the recent release we need not give kernprof.py we can just use kernprof

kernprof -l -v example.py

@shz224
Copy link

shz224 commented Jun 10, 2020

Thanks for offering such a convenient tool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment