Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2....
File: profile.py
#! /usr/bin/python2.7
[0] Fix | Delete
#
[1] Fix | Delete
# Class for profiling python code. rev 1.0 6/2/94
[2] Fix | Delete
#
[3] Fix | Delete
# Written by James Roskind
[4] Fix | Delete
# Based on prior profile module by Sjoerd Mullender...
[5] Fix | Delete
# which was hacked somewhat by: Guido van Rossum
[6] Fix | Delete
[7] Fix | Delete
"""Class for profiling Python code."""
[8] Fix | Delete
[9] Fix | Delete
# Copyright Disney Enterprises, Inc. All Rights Reserved.
[10] Fix | Delete
# Licensed to PSF under a Contributor Agreement
[11] Fix | Delete
#
[12] Fix | Delete
# Licensed under the Apache License, Version 2.0 (the "License");
[13] Fix | Delete
# you may not use this file except in compliance with the License.
[14] Fix | Delete
# You may obtain a copy of the License at
[15] Fix | Delete
#
[16] Fix | Delete
# http://www.apache.org/licenses/LICENSE-2.0
[17] Fix | Delete
#
[18] Fix | Delete
# Unless required by applicable law or agreed to in writing, software
[19] Fix | Delete
# distributed under the License is distributed on an "AS IS" BASIS,
[20] Fix | Delete
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
[21] Fix | Delete
# either express or implied. See the License for the specific language
[22] Fix | Delete
# governing permissions and limitations under the License.
[23] Fix | Delete
[24] Fix | Delete
[25] Fix | Delete
import sys
[26] Fix | Delete
import os
[27] Fix | Delete
import time
[28] Fix | Delete
import marshal
[29] Fix | Delete
from optparse import OptionParser
[30] Fix | Delete
[31] Fix | Delete
__all__ = ["run", "runctx", "help", "Profile"]
[32] Fix | Delete
[33] Fix | Delete
# Sample timer for use with
[34] Fix | Delete
#i_count = 0
[35] Fix | Delete
#def integer_timer():
[36] Fix | Delete
# global i_count
[37] Fix | Delete
# i_count = i_count + 1
[38] Fix | Delete
# return i_count
[39] Fix | Delete
#itimes = integer_timer # replace with C coded timer returning integers
[40] Fix | Delete
[41] Fix | Delete
#**************************************************************************
[42] Fix | Delete
# The following are the static member functions for the profiler class
[43] Fix | Delete
# Note that an instance of Profile() is *not* needed to call them.
[44] Fix | Delete
#**************************************************************************
[45] Fix | Delete
[46] Fix | Delete
def run(statement, filename=None, sort=-1):
[47] Fix | Delete
"""Run statement under profiler optionally saving results in filename
[48] Fix | Delete
[49] Fix | Delete
This function takes a single argument that can be passed to the
[50] Fix | Delete
"exec" statement, and an optional file name. In all cases this
[51] Fix | Delete
routine attempts to "exec" its first argument and gather profiling
[52] Fix | Delete
statistics from the execution. If no file name is present, then this
[53] Fix | Delete
function automatically prints a simple profiling report, sorted by the
[54] Fix | Delete
standard name string (file/line/function-name) that is presented in
[55] Fix | Delete
each line.
[56] Fix | Delete
"""
[57] Fix | Delete
prof = Profile()
[58] Fix | Delete
try:
[59] Fix | Delete
prof = prof.run(statement)
[60] Fix | Delete
except SystemExit:
[61] Fix | Delete
pass
[62] Fix | Delete
if filename is not None:
[63] Fix | Delete
prof.dump_stats(filename)
[64] Fix | Delete
else:
[65] Fix | Delete
return prof.print_stats(sort)
[66] Fix | Delete
[67] Fix | Delete
def runctx(statement, globals, locals, filename=None, sort=-1):
[68] Fix | Delete
"""Run statement under profiler, supplying your own globals and locals,
[69] Fix | Delete
optionally saving results in filename.
[70] Fix | Delete
[71] Fix | Delete
statement and filename have the same semantics as profile.run
[72] Fix | Delete
"""
[73] Fix | Delete
prof = Profile()
[74] Fix | Delete
try:
[75] Fix | Delete
prof = prof.runctx(statement, globals, locals)
[76] Fix | Delete
except SystemExit:
[77] Fix | Delete
pass
[78] Fix | Delete
[79] Fix | Delete
if filename is not None:
[80] Fix | Delete
prof.dump_stats(filename)
[81] Fix | Delete
else:
[82] Fix | Delete
return prof.print_stats(sort)
[83] Fix | Delete
[84] Fix | Delete
# Backwards compatibility.
[85] Fix | Delete
def help():
[86] Fix | Delete
print "Documentation for the profile module can be found "
[87] Fix | Delete
print "in the Python Library Reference, section 'The Python Profiler'."
[88] Fix | Delete
[89] Fix | Delete
if hasattr(os, "times"):
[90] Fix | Delete
def _get_time_times(timer=os.times):
[91] Fix | Delete
t = timer()
[92] Fix | Delete
return t[0] + t[1]
[93] Fix | Delete
[94] Fix | Delete
# Using getrusage(3) is better than clock(3) if available:
[95] Fix | Delete
# on some systems (e.g. FreeBSD), getrusage has a higher resolution
[96] Fix | Delete
# Furthermore, on a POSIX system, returns microseconds, which
[97] Fix | Delete
# wrap around after 36min.
[98] Fix | Delete
_has_res = 0
[99] Fix | Delete
try:
[100] Fix | Delete
import resource
[101] Fix | Delete
resgetrusage = lambda: resource.getrusage(resource.RUSAGE_SELF)
[102] Fix | Delete
def _get_time_resource(timer=resgetrusage):
[103] Fix | Delete
t = timer()
[104] Fix | Delete
return t[0] + t[1]
[105] Fix | Delete
_has_res = 1
[106] Fix | Delete
except ImportError:
[107] Fix | Delete
pass
[108] Fix | Delete
[109] Fix | Delete
class Profile:
[110] Fix | Delete
"""Profiler class.
[111] Fix | Delete
[112] Fix | Delete
self.cur is always a tuple. Each such tuple corresponds to a stack
[113] Fix | Delete
frame that is currently active (self.cur[-2]). The following are the
[114] Fix | Delete
definitions of its members. We use this external "parallel stack" to
[115] Fix | Delete
avoid contaminating the program that we are profiling. (old profiler
[116] Fix | Delete
used to write into the frames local dictionary!!) Derived classes
[117] Fix | Delete
can change the definition of some entries, as long as they leave
[118] Fix | Delete
[-2:] intact (frame and previous tuple). In case an internal error is
[119] Fix | Delete
detected, the -3 element is used as the function name.
[120] Fix | Delete
[121] Fix | Delete
[ 0] = Time that needs to be charged to the parent frame's function.
[122] Fix | Delete
It is used so that a function call will not have to access the
[123] Fix | Delete
timing data for the parent frame.
[124] Fix | Delete
[ 1] = Total time spent in this frame's function, excluding time in
[125] Fix | Delete
subfunctions (this latter is tallied in cur[2]).
[126] Fix | Delete
[ 2] = Total time spent in subfunctions, excluding time executing the
[127] Fix | Delete
frame's function (this latter is tallied in cur[1]).
[128] Fix | Delete
[-3] = Name of the function that corresponds to this frame.
[129] Fix | Delete
[-2] = Actual frame that we correspond to (used to sync exception handling).
[130] Fix | Delete
[-1] = Our parent 6-tuple (corresponds to frame.f_back).
[131] Fix | Delete
[132] Fix | Delete
Timing data for each function is stored as a 5-tuple in the dictionary
[133] Fix | Delete
self.timings[]. The index is always the name stored in self.cur[-3].
[134] Fix | Delete
The following are the definitions of the members:
[135] Fix | Delete
[136] Fix | Delete
[0] = The number of times this function was called, not counting direct
[137] Fix | Delete
or indirect recursion,
[138] Fix | Delete
[1] = Number of times this function appears on the stack, minus one
[139] Fix | Delete
[2] = Total time spent internal to this function
[140] Fix | Delete
[3] = Cumulative time that this function was present on the stack. In
[141] Fix | Delete
non-recursive functions, this is the total execution time from start
[142] Fix | Delete
to finish of each invocation of a function, including time spent in
[143] Fix | Delete
all subfunctions.
[144] Fix | Delete
[4] = A dictionary indicating for each function name, the number of times
[145] Fix | Delete
it was called by us.
[146] Fix | Delete
"""
[147] Fix | Delete
[148] Fix | Delete
bias = 0 # calibration constant
[149] Fix | Delete
[150] Fix | Delete
def __init__(self, timer=None, bias=None):
[151] Fix | Delete
self.timings = {}
[152] Fix | Delete
self.cur = None
[153] Fix | Delete
self.cmd = ""
[154] Fix | Delete
self.c_func_name = ""
[155] Fix | Delete
[156] Fix | Delete
if bias is None:
[157] Fix | Delete
bias = self.bias
[158] Fix | Delete
self.bias = bias # Materialize in local dict for lookup speed.
[159] Fix | Delete
[160] Fix | Delete
if not timer:
[161] Fix | Delete
if _has_res:
[162] Fix | Delete
self.timer = resgetrusage
[163] Fix | Delete
self.dispatcher = self.trace_dispatch
[164] Fix | Delete
self.get_time = _get_time_resource
[165] Fix | Delete
elif hasattr(time, 'clock'):
[166] Fix | Delete
self.timer = self.get_time = time.clock
[167] Fix | Delete
self.dispatcher = self.trace_dispatch_i
[168] Fix | Delete
elif hasattr(os, 'times'):
[169] Fix | Delete
self.timer = os.times
[170] Fix | Delete
self.dispatcher = self.trace_dispatch
[171] Fix | Delete
self.get_time = _get_time_times
[172] Fix | Delete
else:
[173] Fix | Delete
self.timer = self.get_time = time.time
[174] Fix | Delete
self.dispatcher = self.trace_dispatch_i
[175] Fix | Delete
else:
[176] Fix | Delete
self.timer = timer
[177] Fix | Delete
t = self.timer() # test out timer function
[178] Fix | Delete
try:
[179] Fix | Delete
length = len(t)
[180] Fix | Delete
except TypeError:
[181] Fix | Delete
self.get_time = timer
[182] Fix | Delete
self.dispatcher = self.trace_dispatch_i
[183] Fix | Delete
else:
[184] Fix | Delete
if length == 2:
[185] Fix | Delete
self.dispatcher = self.trace_dispatch
[186] Fix | Delete
else:
[187] Fix | Delete
self.dispatcher = self.trace_dispatch_l
[188] Fix | Delete
# This get_time() implementation needs to be defined
[189] Fix | Delete
# here to capture the passed-in timer in the parameter
[190] Fix | Delete
# list (for performance). Note that we can't assume
[191] Fix | Delete
# the timer() result contains two values in all
[192] Fix | Delete
# cases.
[193] Fix | Delete
def get_time_timer(timer=timer, sum=sum):
[194] Fix | Delete
return sum(timer())
[195] Fix | Delete
self.get_time = get_time_timer
[196] Fix | Delete
self.t = self.get_time()
[197] Fix | Delete
self.simulate_call('profiler')
[198] Fix | Delete
[199] Fix | Delete
# Heavily optimized dispatch routine for os.times() timer
[200] Fix | Delete
[201] Fix | Delete
def trace_dispatch(self, frame, event, arg):
[202] Fix | Delete
timer = self.timer
[203] Fix | Delete
t = timer()
[204] Fix | Delete
t = t[0] + t[1] - self.t - self.bias
[205] Fix | Delete
[206] Fix | Delete
if event == "c_call":
[207] Fix | Delete
self.c_func_name = arg.__name__
[208] Fix | Delete
[209] Fix | Delete
if self.dispatch[event](self, frame,t):
[210] Fix | Delete
t = timer()
[211] Fix | Delete
self.t = t[0] + t[1]
[212] Fix | Delete
else:
[213] Fix | Delete
r = timer()
[214] Fix | Delete
self.t = r[0] + r[1] - t # put back unrecorded delta
[215] Fix | Delete
[216] Fix | Delete
# Dispatch routine for best timer program (return = scalar, fastest if
[217] Fix | Delete
# an integer but float works too -- and time.clock() relies on that).
[218] Fix | Delete
[219] Fix | Delete
def trace_dispatch_i(self, frame, event, arg):
[220] Fix | Delete
timer = self.timer
[221] Fix | Delete
t = timer() - self.t - self.bias
[222] Fix | Delete
[223] Fix | Delete
if event == "c_call":
[224] Fix | Delete
self.c_func_name = arg.__name__
[225] Fix | Delete
[226] Fix | Delete
if self.dispatch[event](self, frame, t):
[227] Fix | Delete
self.t = timer()
[228] Fix | Delete
else:
[229] Fix | Delete
self.t = timer() - t # put back unrecorded delta
[230] Fix | Delete
[231] Fix | Delete
# Dispatch routine for macintosh (timer returns time in ticks of
[232] Fix | Delete
# 1/60th second)
[233] Fix | Delete
[234] Fix | Delete
def trace_dispatch_mac(self, frame, event, arg):
[235] Fix | Delete
timer = self.timer
[236] Fix | Delete
t = timer()/60.0 - self.t - self.bias
[237] Fix | Delete
[238] Fix | Delete
if event == "c_call":
[239] Fix | Delete
self.c_func_name = arg.__name__
[240] Fix | Delete
[241] Fix | Delete
if self.dispatch[event](self, frame, t):
[242] Fix | Delete
self.t = timer()/60.0
[243] Fix | Delete
else:
[244] Fix | Delete
self.t = timer()/60.0 - t # put back unrecorded delta
[245] Fix | Delete
[246] Fix | Delete
# SLOW generic dispatch routine for timer returning lists of numbers
[247] Fix | Delete
[248] Fix | Delete
def trace_dispatch_l(self, frame, event, arg):
[249] Fix | Delete
get_time = self.get_time
[250] Fix | Delete
t = get_time() - self.t - self.bias
[251] Fix | Delete
[252] Fix | Delete
if event == "c_call":
[253] Fix | Delete
self.c_func_name = arg.__name__
[254] Fix | Delete
[255] Fix | Delete
if self.dispatch[event](self, frame, t):
[256] Fix | Delete
self.t = get_time()
[257] Fix | Delete
else:
[258] Fix | Delete
self.t = get_time() - t # put back unrecorded delta
[259] Fix | Delete
[260] Fix | Delete
# In the event handlers, the first 3 elements of self.cur are unpacked
[261] Fix | Delete
# into vrbls w/ 3-letter names. The last two characters are meant to be
[262] Fix | Delete
# mnemonic:
[263] Fix | Delete
# _pt self.cur[0] "parent time" time to be charged to parent frame
[264] Fix | Delete
# _it self.cur[1] "internal time" time spent directly in the function
[265] Fix | Delete
# _et self.cur[2] "external time" time spent in subfunctions
[266] Fix | Delete
[267] Fix | Delete
def trace_dispatch_exception(self, frame, t):
[268] Fix | Delete
rpt, rit, ret, rfn, rframe, rcur = self.cur
[269] Fix | Delete
if (rframe is not frame) and rcur:
[270] Fix | Delete
return self.trace_dispatch_return(rframe, t)
[271] Fix | Delete
self.cur = rpt, rit+t, ret, rfn, rframe, rcur
[272] Fix | Delete
return 1
[273] Fix | Delete
[274] Fix | Delete
[275] Fix | Delete
def trace_dispatch_call(self, frame, t):
[276] Fix | Delete
if self.cur and frame.f_back is not self.cur[-2]:
[277] Fix | Delete
rpt, rit, ret, rfn, rframe, rcur = self.cur
[278] Fix | Delete
if not isinstance(rframe, Profile.fake_frame):
[279] Fix | Delete
assert rframe.f_back is frame.f_back, ("Bad call", rfn,
[280] Fix | Delete
rframe, rframe.f_back,
[281] Fix | Delete
frame, frame.f_back)
[282] Fix | Delete
self.trace_dispatch_return(rframe, 0)
[283] Fix | Delete
assert (self.cur is None or \
[284] Fix | Delete
frame.f_back is self.cur[-2]), ("Bad call",
[285] Fix | Delete
self.cur[-3])
[286] Fix | Delete
fcode = frame.f_code
[287] Fix | Delete
fn = (fcode.co_filename, fcode.co_firstlineno, fcode.co_name)
[288] Fix | Delete
self.cur = (t, 0, 0, fn, frame, self.cur)
[289] Fix | Delete
timings = self.timings
[290] Fix | Delete
if fn in timings:
[291] Fix | Delete
cc, ns, tt, ct, callers = timings[fn]
[292] Fix | Delete
timings[fn] = cc, ns + 1, tt, ct, callers
[293] Fix | Delete
else:
[294] Fix | Delete
timings[fn] = 0, 0, 0, 0, {}
[295] Fix | Delete
return 1
[296] Fix | Delete
[297] Fix | Delete
def trace_dispatch_c_call (self, frame, t):
[298] Fix | Delete
fn = ("", 0, self.c_func_name)
[299] Fix | Delete
self.cur = (t, 0, 0, fn, frame, self.cur)
[300] Fix | Delete
timings = self.timings
[301] Fix | Delete
if fn in timings:
[302] Fix | Delete
cc, ns, tt, ct, callers = timings[fn]
[303] Fix | Delete
timings[fn] = cc, ns+1, tt, ct, callers
[304] Fix | Delete
else:
[305] Fix | Delete
timings[fn] = 0, 0, 0, 0, {}
[306] Fix | Delete
return 1
[307] Fix | Delete
[308] Fix | Delete
def trace_dispatch_return(self, frame, t):
[309] Fix | Delete
if frame is not self.cur[-2]:
[310] Fix | Delete
assert frame is self.cur[-2].f_back, ("Bad return", self.cur[-3])
[311] Fix | Delete
self.trace_dispatch_return(self.cur[-2], 0)
[312] Fix | Delete
[313] Fix | Delete
# Prefix "r" means part of the Returning or exiting frame.
[314] Fix | Delete
# Prefix "p" means part of the Previous or Parent or older frame.
[315] Fix | Delete
[316] Fix | Delete
rpt, rit, ret, rfn, frame, rcur = self.cur
[317] Fix | Delete
rit = rit + t
[318] Fix | Delete
frame_total = rit + ret
[319] Fix | Delete
[320] Fix | Delete
ppt, pit, pet, pfn, pframe, pcur = rcur
[321] Fix | Delete
self.cur = ppt, pit + rpt, pet + frame_total, pfn, pframe, pcur
[322] Fix | Delete
[323] Fix | Delete
timings = self.timings
[324] Fix | Delete
cc, ns, tt, ct, callers = timings[rfn]
[325] Fix | Delete
if not ns:
[326] Fix | Delete
# This is the only occurrence of the function on the stack.
[327] Fix | Delete
# Else this is a (directly or indirectly) recursive call, and
[328] Fix | Delete
# its cumulative time will get updated when the topmost call to
[329] Fix | Delete
# it returns.
[330] Fix | Delete
ct = ct + frame_total
[331] Fix | Delete
cc = cc + 1
[332] Fix | Delete
[333] Fix | Delete
if pfn in callers:
[334] Fix | Delete
callers[pfn] = callers[pfn] + 1 # hack: gather more
[335] Fix | Delete
# stats such as the amount of time added to ct courtesy
[336] Fix | Delete
# of this specific call, and the contribution to cc
[337] Fix | Delete
# courtesy of this call.
[338] Fix | Delete
else:
[339] Fix | Delete
callers[pfn] = 1
[340] Fix | Delete
[341] Fix | Delete
timings[rfn] = cc, ns - 1, tt + rit, ct, callers
[342] Fix | Delete
[343] Fix | Delete
return 1
[344] Fix | Delete
[345] Fix | Delete
[346] Fix | Delete
dispatch = {
[347] Fix | Delete
"call": trace_dispatch_call,
[348] Fix | Delete
"exception": trace_dispatch_exception,
[349] Fix | Delete
"return": trace_dispatch_return,
[350] Fix | Delete
"c_call": trace_dispatch_c_call,
[351] Fix | Delete
"c_exception": trace_dispatch_return, # the C function returned
[352] Fix | Delete
"c_return": trace_dispatch_return,
[353] Fix | Delete
}
[354] Fix | Delete
[355] Fix | Delete
[356] Fix | Delete
# The next few functions play with self.cmd. By carefully preloading
[357] Fix | Delete
# our parallel stack, we can force the profiled result to include
[358] Fix | Delete
# an arbitrary string as the name of the calling function.
[359] Fix | Delete
# We use self.cmd as that string, and the resulting stats look
[360] Fix | Delete
# very nice :-).
[361] Fix | Delete
[362] Fix | Delete
def set_cmd(self, cmd):
[363] Fix | Delete
if self.cur[-1]: return # already set
[364] Fix | Delete
self.cmd = cmd
[365] Fix | Delete
self.simulate_call(cmd)
[366] Fix | Delete
[367] Fix | Delete
class fake_code:
[368] Fix | Delete
def __init__(self, filename, line, name):
[369] Fix | Delete
self.co_filename = filename
[370] Fix | Delete
self.co_line = line
[371] Fix | Delete
self.co_name = name
[372] Fix | Delete
self.co_firstlineno = 0
[373] Fix | Delete
[374] Fix | Delete
def __repr__(self):
[375] Fix | Delete
return repr((self.co_filename, self.co_line, self.co_name))
[376] Fix | Delete
[377] Fix | Delete
class fake_frame:
[378] Fix | Delete
def __init__(self, code, prior):
[379] Fix | Delete
self.f_code = code
[380] Fix | Delete
self.f_back = prior
[381] Fix | Delete
[382] Fix | Delete
def simulate_call(self, name):
[383] Fix | Delete
code = self.fake_code('profile', 0, name)
[384] Fix | Delete
if self.cur:
[385] Fix | Delete
pframe = self.cur[-2]
[386] Fix | Delete
else:
[387] Fix | Delete
pframe = None
[388] Fix | Delete
frame = self.fake_frame(code, pframe)
[389] Fix | Delete
self.dispatch['call'](self, frame, 0)
[390] Fix | Delete
[391] Fix | Delete
# collect stats from pending stack, including getting final
[392] Fix | Delete
# timings for self.cmd frame.
[393] Fix | Delete
[394] Fix | Delete
def simulate_cmd_complete(self):
[395] Fix | Delete
get_time = self.get_time
[396] Fix | Delete
t = get_time() - self.t
[397] Fix | Delete
while self.cur[-1]:
[398] Fix | Delete
# We *can* cause assertion errors here if
[399] Fix | Delete
# dispatch_trace_return checks for a frame match!
[400] Fix | Delete
self.dispatch['return'](self, self.cur[-2], t)
[401] Fix | Delete
t = 0
[402] Fix | Delete
self.t = get_time() - t
[403] Fix | Delete
[404] Fix | Delete
[405] Fix | Delete
def print_stats(self, sort=-1):
[406] Fix | Delete
import pstats
[407] Fix | Delete
pstats.Stats(self).strip_dirs().sort_stats(sort). \
[408] Fix | Delete
print_stats()
[409] Fix | Delete
[410] Fix | Delete
def dump_stats(self, file):
[411] Fix | Delete
f = open(file, 'wb')
[412] Fix | Delete
self.create_stats()
[413] Fix | Delete
marshal.dump(self.stats, f)
[414] Fix | Delete
f.close()
[415] Fix | Delete
[416] Fix | Delete
def create_stats(self):
[417] Fix | Delete
self.simulate_cmd_complete()
[418] Fix | Delete
self.snapshot_stats()
[419] Fix | Delete
[420] Fix | Delete
def snapshot_stats(self):
[421] Fix | Delete
self.stats = {}
[422] Fix | Delete
for func, (cc, ns, tt, ct, callers) in self.timings.iteritems():
[423] Fix | Delete
callers = callers.copy()
[424] Fix | Delete
nc = 0
[425] Fix | Delete
for callcnt in callers.itervalues():
[426] Fix | Delete
nc += callcnt
[427] Fix | Delete
self.stats[func] = cc, nc, tt, ct, callers
[428] Fix | Delete
[429] Fix | Delete
[430] Fix | Delete
# The following two methods can be called by clients to use
[431] Fix | Delete
# a profiler to profile a statement, given as a string.
[432] Fix | Delete
[433] Fix | Delete
def run(self, cmd):
[434] Fix | Delete
import __main__
[435] Fix | Delete
dict = __main__.__dict__
[436] Fix | Delete
return self.runctx(cmd, dict, dict)
[437] Fix | Delete
[438] Fix | Delete
def runctx(self, cmd, globals, locals):
[439] Fix | Delete
self.set_cmd(cmd)
[440] Fix | Delete
sys.setprofile(self.dispatcher)
[441] Fix | Delete
try:
[442] Fix | Delete
exec cmd in globals, locals
[443] Fix | Delete
finally:
[444] Fix | Delete
sys.setprofile(None)
[445] Fix | Delete
return self
[446] Fix | Delete
[447] Fix | Delete
# This method is more useful to profile a single function call.
[448] Fix | Delete
def runcall(self, func, *args, **kw):
[449] Fix | Delete
self.set_cmd(repr(func))
[450] Fix | Delete
sys.setprofile(self.dispatcher)
[451] Fix | Delete
try:
[452] Fix | Delete
return func(*args, **kw)
[453] Fix | Delete
finally:
[454] Fix | Delete
sys.setprofile(None)
[455] Fix | Delete
[456] Fix | Delete
[457] Fix | Delete
#******************************************************************
[458] Fix | Delete
# The following calculates the overhead for using a profiler. The
[459] Fix | Delete
# problem is that it takes a fair amount of time for the profiler
[460] Fix | Delete
# to stop the stopwatch (from the time it receives an event).
[461] Fix | Delete
# Similarly, there is a delay from the time that the profiler
[462] Fix | Delete
# re-starts the stopwatch before the user's code really gets to
[463] Fix | Delete
# continue. The following code tries to measure the difference on
[464] Fix | Delete
# a per-event basis.
[465] Fix | Delete
#
[466] Fix | Delete
# Note that this difference is only significant if there are a lot of
[467] Fix | Delete
# events, and relatively little user code per event. For example,
[468] Fix | Delete
# code with small functions will typically benefit from having the
[469] Fix | Delete
# profiler calibrated for the current platform. This *could* be
[470] Fix | Delete
# done on the fly during init() time, but it is not worth the
[471] Fix | Delete
# effort. Also note that if too large a value specified, then
[472] Fix | Delete
# execution time on some functions will actually appear as a
[473] Fix | Delete
# negative number. It is *normal* for some functions (with very
[474] Fix | Delete
# low call counts) to have such negative stats, even if the
[475] Fix | Delete
# calibration figure is "correct."
[476] Fix | Delete
#
[477] Fix | Delete
# One alternative to profile-time calibration adjustments (i.e.,
[478] Fix | Delete
# adding in the magic little delta during each event) is to track
[479] Fix | Delete
# more carefully the number of events (and cumulatively, the number
[480] Fix | Delete
# of events during sub functions) that are seen. If this were
[481] Fix | Delete
# done, then the arithmetic could be done after the fact (i.e., at
[482] Fix | Delete
# display time). Currently, we track only call/return events.
[483] Fix | Delete
# These values can be deduced by examining the callees and callers
[484] Fix | Delete
# vectors for each functions. Hence we *can* almost correct the
[485] Fix | Delete
# internal time figure at print time (note that we currently don't
[486] Fix | Delete
# track exception event processing counts). Unfortunately, there
[487] Fix | Delete
# is currently no similar information for cumulative sub-function
[488] Fix | Delete
# time. It would not be hard to "get all this info" at profiler
[489] Fix | Delete
# time. Specifically, we would have to extend the tuples to keep
[490] Fix | Delete
# counts of this in each frame, and then extend the defs of timing
[491] Fix | Delete
# tuples to include the significant two figures. I'm a bit fearful
[492] Fix | Delete
# that this additional feature will slow the heavily optimized
[493] Fix | Delete
# event/time ratio (i.e., the profiler would run slower, fur a very
[494] Fix | Delete
# low "value added" feature.)
[495] Fix | Delete
#**************************************************************
[496] Fix | Delete
[497] Fix | Delete
def calibrate(self, m, verbose=0):
[498] Fix | Delete
if self.__class__ is not Profile:
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function