profiler_file.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # VARS = --none--
  4. profiler_file_string = """
  5. #!/usr/bin/env python
  6. # encoding: utf-8
  7. import cProfile, pstats, StringIO
  8. def profile():
  9. #------------------------------------------------------------------------------
  10. # Setup a profile
  11. #------------------------------------------------------------------------------
  12. pr = cProfile.Profile()
  13. #------------------------------------------------------------------------------
  14. # Enter setup code below
  15. #------------------------------------------------------------------------------
  16. # Optional: include setup code here
  17. #------------------------------------------------------------------------------
  18. # Start profiler
  19. #------------------------------------------------------------------------------
  20. pr.enable()
  21. #------------------------------------------------------------------------------
  22. # BEGIN profiled code block
  23. #------------------------------------------------------------------------------
  24. # include profiled code here
  25. #------------------------------------------------------------------------------
  26. # END profiled code block
  27. #------------------------------------------------------------------------------
  28. pr.disable()
  29. s = StringIO.StringIO()
  30. sortby = 'cumulative'
  31. ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
  32. ps.strip_dirs().sort_stats("time").print_stats()
  33. print(s.getvalue())
  34. if __name__ == '__main__':
  35. profile()
  36. """