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