changeset 553:3622a5653ee9

added basic info and function to profile code
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 08 Jul 2014 17:23:02 -0400
parents ca6bded754ac
children 7d051afcb22d
files python/utils.py
diffstat 1 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/python/utils.py	Tue Jul 08 17:04:55 2014 -0400
+++ b/python/utils.py	Tue Jul 08 17:23:02 2014 -0400
@@ -552,6 +552,30 @@
 
 
 #########################
+# Profiling
+#########################
+
+def analyzeProfile(profileFilename, stripDirs = True):
+    '''Analyze the file produced by cProfile 
+
+    obtained by for example: 
+    - call in script (for main() function in script)
+    import cProfile, os
+    cProfile.run('main()', os.path.join(os.getcwd(),'main.profile'))
+
+    - or on the command line:
+    python -m cProfile [-o profile.bin] [-s sort] scriptfile [arg]'''
+    import pstats, os
+    p = pstats.Stats(os.path.join(os.pardir, profileFilename))
+    if stripDirs:
+        p.strip_dirs()
+    p.sort_stats('time')
+    p.print_stats(.2)
+    #p.sort_stats('time')
+    # p.print_callees(.1, 'int_prediction.py:')
+    return p
+
+#########################
 # running tests
 #########################