Skip to content

Instantly share code, notes, and snippets.

@Ryanb58
Created May 22, 2018 15:51
Show Gist options
  • Save Ryanb58/a44d4a3742b3c53a95b0eb60c1a2ddca to your computer and use it in GitHub Desktop.
Save Ryanb58/a44d4a3742b3c53a95b0eb60c1a2ddca to your computer and use it in GitHub Desktop.
Profile a section of code in python.
import time
import cProfile, pstats, StringIO
import os
import shutil
import tempfile
start_time = time.time()
pr = cProfile.Profile()
pr.enable()
# Insert function you want called here.
pr.disable()
sortby = 'cumulative'
fd, tmpfile = tempfile.mkstemp()
os.close(fd)
pr.dump_stats(tmpfile)
shutil.move(tmpfile, '/usr/local/smartfile/www/CPROFILE')
print("Function took {} seconds.".format(time.time() - start_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment