python 性能调试 cprofile hotshot


1. cprofile

#!/usr/bin/env python
# encoding: utf-8 # filename: profile.py import pstats, cProfile import calc_pi cProfile.runctx("calc_pi.approx_pi()", globals(), locals(), "Profile.prof") s = pstats.Stats("Profile.prof") s.strip_dirs().sort_stats("time").print_stats()


2.hotshot

import hotshot
import hotshot.stats
prof = hotshot.Profile("hs_prof.txt", 1)
prof.runcall(func, *args, **args2)
prof.close()
p = hotshot.stats.load("hs_prof.txt")
p.strip_dirs()
p.sort_stats('cumtime', 'calls')
p.print_stats(20)

 

分享到: 微信 更多