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