Last active
February 26, 2022 07:20
-
-
Save danriti/8015889 to your computer and use it in GitHub Desktop.
Line Profiling in Python
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
#!/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() |
exactly
You have to call kernprof with the -l flag for the @Profile decorator to be recognized
What is kernprof.py?
In the recent release we need not give kernprof.py we can just use kernprof
kernprof -l -v example.py
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
Where is "@Profile" decorator defined?