@@ -47,37 +47,37 @@ class _Utils:
47
47
def __init__ (self , profiler ):
48
48
self .profiler = profiler
49
49
50
- def run (self , statement , filename , sort ):
50
+ def run (self , statement , filename , sort , numresults ):
51
51
prof = self .profiler ()
52
52
try :
53
53
prof .run (statement )
54
54
except SystemExit :
55
55
pass
56
56
finally :
57
- self ._show (prof , filename , sort )
57
+ self ._show (prof , filename , sort , numresults )
58
58
59
- def runctx (self , statement , globals , locals , filename , sort ):
59
+ def runctx (self , statement , globals , locals , filename , sort , numresults ):
60
60
prof = self .profiler ()
61
61
try :
62
62
prof .runctx (statement , globals , locals )
63
63
except SystemExit :
64
64
pass
65
65
finally :
66
- self ._show (prof , filename , sort )
66
+ self ._show (prof , filename , sort , numresults )
67
67
68
- def _show (self , prof , filename , sort ):
68
+ def _show (self , prof , filename , sort , numresults ):
69
69
if filename is not None :
70
70
prof .dump_stats (filename )
71
71
else :
72
- prof .print_stats (sort )
72
+ prof .print_stats (sort , numresults )
73
73
74
74
75
75
#**************************************************************************
76
76
# The following are the static member functions for the profiler class
77
77
# Note that an instance of Profile() is *not* needed to call them.
78
78
#**************************************************************************
79
79
80
- def run (statement , filename = None , sort = - 1 ):
80
+ def run (statement , filename = None , sort = - 1 , numresults = None ):
81
81
"""Run statement under profiler optionally saving results in filename
82
82
83
83
This function takes a single argument that can be passed to the
@@ -90,13 +90,15 @@ def run(statement, filename=None, sort=-1):
90
90
"""
91
91
return _Utils (Profile ).run (statement , filename , sort )
92
92
93
- def runctx (statement , globals , locals , filename = None , sort = - 1 ):
93
+ def runctx (statement , globals , locals , filename = None , sort = - 1 ,
94
+ numresults = None ):
94
95
"""Run statement under profiler, supplying your own globals and locals,
95
96
optionally saving results in filename.
96
97
97
98
statement and filename have the same semantics as profile.run
98
99
"""
99
- return _Utils (Profile ).runctx (statement , globals , locals , filename , sort )
100
+ return _Utils (Profile ).runctx (statement , globals , locals , filename , sort ,
101
+ numresults )
100
102
101
103
102
104
class Profile :
@@ -383,10 +385,10 @@ def simulate_cmd_complete(self):
383
385
self .t = get_time () - t
384
386
385
387
386
- def print_stats (self , sort = - 1 ):
388
+ def print_stats (self , sort = - 1 , numresults = None ):
387
389
import pstats
388
- pstats .Stats (self ).strip_dirs ().sort_stats (sort ). \
389
- print_stats ( )
390
+ pstats .Stats (self ).strip_dirs ().sort_stats (sort ).print_stats (
391
+ numresults )
390
392
391
393
def dump_stats (self , file ):
392
394
with open (file , 'wb' ) as f :
@@ -566,18 +568,21 @@ def f(m, f1=f1):
566
568
567
569
def main ():
568
570
import os
571
+ import pstats
569
572
from optparse import OptionParser
570
573
571
- usage = "profile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..."
574
+ usage = "profile.py [-o output_file_path] [-s sort] [-n limit] [- m module | scriptfile] [arg] ..."
572
575
parser = OptionParser (usage = usage )
573
576
parser .allow_interspersed_args = False
574
577
parser .add_option ('-o' , '--outfile' , dest = "outfile" ,
575
578
help = "Save stats to <outfile>" , default = None )
576
579
parser .add_option ('-m' , dest = "module" , action = "store_true" ,
577
580
help = "Profile a library module." , default = False )
581
+ parser .add_option ('-n' , '--numresults' , dest = "numresults" , type = "int" ,
582
+ help = "Number of results to show" , default = 20 )
578
583
parser .add_option ('-s' , '--sort' , dest = "sort" ,
579
584
help = "Sort order when printing to stdout, based on pstats.Stats class" ,
580
- default = - 1 )
585
+ default = 'time' , choices = sorted ( pstats . Stats . sort_arg_dict_default ) )
581
586
582
587
if not sys .argv [1 :]:
583
588
parser .print_usage ()
@@ -605,7 +610,8 @@ def main():
605
610
'__package__' : None ,
606
611
'__cached__' : None ,
607
612
}
608
- runctx (code , globs , None , options .outfile , options .sort )
613
+ runctx (code , globs , None , options .outfile , options .sort ,
614
+ options .numresults )
609
615
else :
610
616
parser .print_usage ()
611
617
return parser
0 commit comments