Skip to content

Instantly share code, notes, and snippets.

@nhannguyen95
Created April 16, 2018 12:03
Show Gist options
  • Save nhannguyen95/a7a89b40ff417f19d056e2248c3452f3 to your computer and use it in GitHub Desktop.
Save nhannguyen95/a7a89b40ff417f19d056e2248c3452f3 to your computer and use it in GitHub Desktop.
Python profiling
import pstats, cProfile
def recip_square(i):
return 1./i**2
def approx_pi(n=10000000):
val = 0.
for k in range(1,n+1):
val += recip_square(k)
return (6 * val)**.5
if __name__ == '__main__':
cProfile.runctx("approx_pi()", globals(), locals(), "Profile.prof")
s = pstats.Stats("Profile.prof")
s.strip_dirs().sort_stats("time").print_stats()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment