同步操作将从 OpenHarmony-SIG/python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#! /usr/bin/env python3"""Python interface for the 'lsprof' profiler.Compatible with the 'profile' module."""__all__ = ["run", "runctx", "Profile"]import _lsprofimport profile as _pyprofile# ____________________________________________________________# Simple interfacedef run(statement, filename=None, sort=-1):return _pyprofile._Utils(Profile).run(statement, filename, sort)def runctx(statement, globals, locals, filename=None, sort=-1):return _pyprofile._Utils(Profile).runctx(statement, globals, locals,filename, sort)run.__doc__ = _pyprofile.run.__doc__runctx.__doc__ = _pyprofile.runctx.__doc__# ____________________________________________________________class Profile(_lsprof.Profiler):"""Profile(timer=None, timeunit=None, subcalls=True, builtins=True)Builds a profiler object using the specified timer function.The default timer is a fast built-in one based on real time.For custom timer functions returning integers, timeunit canbe a float specifying a scale (i.e. how long each integer unitis, in seconds)."""# Most of the functionality is in the base class.# This subclass only adds convenient and backward-compatible methods.def print_stats(self, sort=-1):import pstatspstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()def dump_stats(self, file):import marshalwith open(file, 'wb') as f:self.create_stats()marshal.dump(self.stats, f)def create_stats(self):self.disable()self.snapshot_stats()def snapshot_stats(self):entries = self.getstats()self.stats = {}callersdicts = {}# call informationfor entry in entries:func = label(entry.code)nc = entry.callcount # ncalls column of pstats (before '/')cc = nc - entry.reccallcount # ncalls column of pstats (after '/')tt = entry.inlinetime # tottime column of pstatsct = entry.totaltime # cumtime column of pstatscallers = {}callersdicts[id(entry.code)] = callersself.stats[func] = cc, nc, tt, ct, callers# subcall informationfor entry in entries:if entry.calls:func = label(entry.code)for subentry in entry.calls:try:callers = callersdicts[id(subentry.code)]except KeyError:continuenc = subentry.callcountcc = nc - subentry.reccallcounttt = subentry.inlinetimect = subentry.totaltimeif func in callers:prev = callers[func]nc += prev[0]cc += prev[1]tt += prev[2]ct += prev[3]callers[func] = nc, cc, tt, ct# The following two methods can be called by clients to use# a profiler to profile a statement, given as a string.def run(self, cmd):import __main__dict = __main__.__dict__return self.runctx(cmd, dict, dict)def runctx(self, cmd, globals, locals):self.enable()try:exec(cmd, globals, locals)finally:self.disable()return self# This method is more useful to profile a single function call.def runcall(*args, **kw):if len(args) >= 2:self, func, *args = argselif not args:raise TypeError("descriptor 'runcall' of 'Profile' object ""needs an argument")elif 'func' in kw:func = kw.pop('func')self, *args = argsimport warningswarnings.warn("Passing 'func' as keyword argument is deprecated",DeprecationWarning, stacklevel=2)else:raise TypeError('runcall expected at least 1 positional argument, ''got %d' % (len(args)-1))self.enable()try:return func(*args, **kw)finally:self.disable()runcall.__text_signature__ = '($self, func, /, *args, **kw)'def __enter__(self):self.enable()return selfdef __exit__(self, *exc_info):self.disable()# ____________________________________________________________def label(code):if isinstance(code, str):return ('~', 0, code) # built-in functions ('~' sorts at the end)else:return (code.co_filename, code.co_firstlineno, code.co_name)# ____________________________________________________________def main():import osimport sysimport runpyimport pstatsfrom optparse import OptionParserusage = "cProfile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..."parser = OptionParser(usage=usage)parser.allow_interspersed_args = Falseparser.add_option('-o', '--outfile', dest="outfile",help="Save stats to <outfile>", default=None)parser.add_option('-s', '--sort', dest="sort",help="Sort order when printing to stdout, based on pstats.Stats class",default=-1,choices=sorted(pstats.Stats.sort_arg_dict_default))parser.add_option('-m', dest="module", action="store_true",help="Profile a library module", default=False)if not sys.argv[1:]:parser.print_usage()sys.exit(2)(options, args) = parser.parse_args()sys.argv[:] = args# The script that we're profiling may chdir, so capture the absolute path# to the output file at startup.if options.outfile is not None:options.outfile = os.path.abspath(options.outfile)if len(args) > 0:if options.module:code = "run_module(modname, run_name='__main__')"globs = {'run_module': runpy.run_module,'modname': args[0]}else:progname = args[0]sys.path.insert(0, os.path.dirname(progname))with open(progname, 'rb') as fp:code = compile(fp.read(), progname, 'exec')globs = {'__file__': progname,'__name__': '__main__','__package__': None,'__cached__': None,}try:runctx(code, globs, None, options.outfile, options.sort)except BrokenPipeError as exc:# Prevent "Exception ignored" during interpreter shutdown.sys.stdout = Nonesys.exit(exc.errno)else:parser.print_usage()return parser# When invoked as main program, invoke the profiler on a scriptif __name__ == '__main__':main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。