我们从Python开源项目中,提取了以下40个代码示例,用于说明如何使用profile.run()。
def __str__(self): pm = '+-' if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding: try: u'\xb1'.encode(sys.stdout.encoding) pm = u'\xb1' except: pass return ( u"{mean} {pm} {std} per loop (mean {pm} std. dev. of {runs} run{run_plural}, {loops} loop{loop_plural} each)" .format( pm = pm, runs = self.repeat, loops = self.loops, loop_plural = "" if self.loops == 1 else "s", run_plural = "" if self.repeat == 1 else "s", mean = _format_time(self.average, self._precision), std = _format_time(self.stdev, self._precision)) )
def runMain(main, options, args): if options.profile: if True: import hotshot profile = hotshot.Profile(options.profile) profile.runcall(main, options, args) profile.close() import hotshot.stats stats = hotshot.stats.load(options.profile) else: import profile profile.run('main(options, args)', options.profile) import pstats stats = pstats.Stats(options.profile) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(20) elif options.psyco: import psyco psyco.full() status = main(options, args) else: status = main(options, args) return status
def _profileWithoutGarbageLeak(cmd, filename): # The profile module isn't necessarily installed on every Python # installation, so we import it here, instead of in the module # scope. import profile # this is necessary because the profile module creates a memory leak Profile = profile.Profile statement = cmd sort = -1 retVal = None #### COPIED FROM profile.run #### prof = Profile() try: prof = prof.run(statement) except SystemExit: pass if filename is not None: prof.dump_stats(filename) else: #return prof.print_stats(sort) #DCR retVal = prof.print_stats(sort) #DCR ################################# # eliminate the garbage leak del prof.dispatcher return retVal
def startProfile(filename=PyUtilProfileDefaultFilename, lines=PyUtilProfileDefaultLines, sorts=PyUtilProfileDefaultSorts, silent=0, callInfo=1, useDisk=False, cmd='run()'): # uniquify the filename to allow multiple processes to profile simultaneously filename = '%s.%s%s' % (filename, randUint31(), randUint31()) if not useDisk: # use a RAM file _installProfileCustomFuncs(filename) _profileWithoutGarbageLeak(cmd, filename) if silent: extractProfile(filename, lines, sorts, callInfo) else: printProfile(filename, lines, sorts, callInfo) if not useDisk: # discard the RAM file _removeProfileCustomFuncs(filename) else: os.remove(filename) # call these to see the results again, as a string or in the log
def __init__(self, bit_name, instance, position_count, nonterminal_adder, nonterminal_instance=0): self.bit_name = bit_name # number of the first bit of this run self.bit_instance = instance # length of this run of bits self.length = 1 self.position_count = position_count self.position_nonterminal_adders = nonterminal_adder # for nonterminals, the nonterminal_instance says the numeric id # of this sub-nonterminal in the current nonterminal. If there # are 4 sub-nonterminals in a nonterminal, they are numbered 0 # to 3. This index is used to index in to the nonterminal storage # associated with the current nonterminal. self.nonterminal_instance = 0
def runprofile(mainfunction, output, timeout = 60): if noprofiler == True: print('ERROR: profiler and/or pstats library missing ! Please install it (probably package named python-profile) before running a profiling !') return False def profileb3(): profile.run(mainfunction, output) # This is the main function for profiling print('=> SAVING MODE\n\n') print('Calibrating the profiler...') cval = calibrateprofile() #print('Found value : %s' % cval) print('Initializing the profiler...') b3main = KThread(target=profileb3) # we open b3 main function with the profiler, in a special killable thread (see below why) print('Will now run the profiling and terminate it in %s seconds. Results will be saved in %s' % (str(timeout), str(output))) print('\nCountdown:') for i in range(0,5): print(str(5-i)) time.sleep(1) print('0\nStarting to profile...') b3main.start() # starting the thread time.sleep(float(timeout)) # after this amount of seconds, the b3 main function gets killed and the profiler will end its job print('\n\nFinishing the profile and saving to the file %s' % str(output)) b3main.kill() # we must end the main function in order for the profiler to output its results (if we didn't launch a thread and just closed the process, it would have done no result) print('=> Profile done ! Exiting...') return True
def __init__(self, shell): super(ExecutionMagics, self).__init__(shell) if profile is None: self.prun = self.profile_missing_notice # Default execution function used to actually run user code. self.default_runner = None
def _run_with_timing(run, nruns): """ Run function `run` and print timing information. Parameters ---------- run : callable Any callable object which takes no argument. nruns : int Number of times to execute `run`. """ twall0 = time.time() if nruns == 1: t0 = clock2() run() t1 = clock2() t_usr = t1[0] - t0[0] t_sys = t1[1] - t0[1] print("\nIPython CPU timings (estimated):") print(" User : %10.2f s." % t_usr) print(" System : %10.2f s." % t_sys) else: runs = range(nruns) t0 = clock2() for nr in runs: run() t1 = clock2() t_usr = t1[0] - t0[0] t_sys = t1[1] - t0[1] print("\nIPython CPU timings (estimated):") print("Total runs performed:", nruns) print(" Times : %10s %10s" % ('Total', 'Per run')) print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)) print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)) twall1 = time.time() print("Wall time: %10.2f s." % (twall1 - twall0))
def capture(self, line, cell): """run the cell, capturing stdout, stderr, and IPython's rich display() calls.""" args = magic_arguments.parse_argstring(self.capture, line) out = not args.no_stdout err = not args.no_stderr disp = not args.no_display with capture_output(out, err, disp) as io: self.shell.run_cell(cell) if args.output: self.shell.user_ns[args.output] = io
def run_profile(): import profile profile.run('for i in range(1): demo()', '/tmp/profile.out') import pstats p = pstats.Stats('/tmp/profile.out') p.strip_dirs().sort_stats('time', 'cum').print_stats(60) p.strip_dirs().sort_stats('cum', 'time').print_stats(60)
def main(): VERBOSE = 0 DISPLAY = 0 PROFILE = 0 CONTINUE = 0 opts, args = getopt.getopt(sys.argv[1:], 'vqdcp') for k, v in opts: if k == '-v': VERBOSE = 1 visitor.ASTVisitor.VERBOSE = visitor.ASTVisitor.VERBOSE + 1 if k == '-q': if sys.platform[:3]=="win": f = open('nul', 'wb') # /dev/null fails on Windows... else: f = open('/dev/null', 'wb') sys.stdout = f if k == '-d': DISPLAY = 1 if k == '-c': CONTINUE = 1 if k == '-p': PROFILE = 1 if not args: print("no files to compile") else: for filename in args: if VERBOSE: print(filename) try: if PROFILE: profile.run('compileFile(%r, %r)' % (filename, DISPLAY), filename + ".prof") else: compileFile(filename, DISPLAY) except SyntaxError as err: print(err) if err.lineno is not None: print(err.lineno) if not CONTINUE: sys.exit(-1)
def main(): VERBOSE = 0 DISPLAY = 0 PROFILE = 0 CONTINUE = 0 opts, args = getopt.getopt(sys.argv[1:], 'vqdcp') for k, v in opts: if k == '-v': VERBOSE = 1 visitor.ASTVisitor.VERBOSE = visitor.ASTVisitor.VERBOSE + 1 if k == '-q': if sys.platform[:3]=="win": f = open('nul', 'wb') # /dev/null fails on Windows... else: f = open('/dev/null', 'wb') sys.stdout = f if k == '-d': DISPLAY = 1 if k == '-c': CONTINUE = 1 if k == '-p': PROFILE = 1 if not args: print "no files to compile" else: for filename in args: if VERBOSE: print filename try: if PROFILE: profile.run('compileFile(%r, %r)' % (filename, DISPLAY), filename + ".prof") else: compileFile(filename, DISPLAY) except SyntaxError, err: print err if err.lineno is not None: print err.lineno if not CONTINUE: sys.exit(-1)
def _profile_(): try: import cProfile as profile except: import profile profile.run('_fulltest_()') #profile.run('_filetest_()')
def run(main, options, args): import sys if options.tempdir: import tempfile, os if os.path.isdir(options.tempdir): tempfile.tempdir = options.tempdir else: raise ValueError('path does not exist', options.tempdir) if options.resource_usage: import datetime, time startTime = datetime.datetime.now() startClock = time.clock() try: status = runMain(main, options, args) except UsageError: status = 1 print >> sys.stdout, "Try '%s --help'" % sys.argv[0] if options.resource_usage: stopTime = datetime.datetime.now() stopClock = time.clock() print >> sys.stderr, 'elapsed time: ', stopTime - startTime print >> sys.stderr, 'processor time: ', datetime.timedelta(seconds=stopClock - startClock) sys.exit(status)
def test5(): import profile profile.run("test4()", "prof.txt") import pstats p = pstats.Stats("prof.txt") p.sort_stats("time").print_stats()
def code_gen_extract_sub_runs_old(sub_runs, vname, start_clean = True): """Write code that assigns bits to vname based on the sub runs. If start_clean is false, we OR in our first stuff. Otherwise we do an assignment for the very first bits extracted.""" # position is the position of the start of the bit run, treated as # a string, so shifts must be adjusted for the width of the run. eol = ';\n' nl = '\n' if start_clean: first = True else: first = False s = '' for (bit,count,position_str, nonterminal_addr) in sub_runs: print("PROCESSING SUBRUN (%s, %d, %s, %s)" % ( bit, count ,position_str, nonterminal_addr)) # control whether or not we do an assignment or and |= in to our # dest var c. if first: bar = '' first = False else: bar = '|' # must shift last "c" by the amount we are or-ing in on this iteration t += "%s=%s<<%s%s" % (vname, vname, str(count), eol) s += t print("ADDING SHIFT OF PREV STUFF: %s" % t) sindex = str(position_str) if nonterminal_addr != '': sindex += nonterminal_addr sindex += '+xed_decoded_inst_nonterminal_bitpos_start(xds)' s += "%s %s=xed_decoded_inst_read_any_bits(xds,%s,%s)%s" % ( vname, bar, sindex, str(count), eol ) return s
def subprocessprofileb3(profiler, mainfunction, output): #b3thread = KThread(target=profileb3_timer) #b3thread.start() profiler.run(mainfunction)
def runprofilesubprocess(mainfunction, output, timeout = 60): # Testing function for profiling, using a subprocess (does not really work because of how cProfile works) try: print('PROFILER SAVING MODE\n--------------------\n') print('Preparing the profiler...') #b3main = profileb3_thread() #b3thread = KThread(target=profileb3_timer) #b3thread.start() profiler = cProfile.Profile() b3main = multiprocessing.Process(target=subprocessprofileb3, args=(profiler, mainfunction,output)) print('Will now run the profiling and terminate it in %s seconds. Results will be saved in %s' % (str(timeout), str(output))) print('\nCountdown:') for i in range(0,6): print(str(5-i)) time.sleep(1) print('Starting to profile...') #profileb3("""b3.tools.profile.subb3()""", output) b3main.start() # b3main.start() # starting the thread time.sleep(float(timeout)) # after this amount of seconds, the b3 main function gets killed and the profiler will end its job print('\n\nFinishing the profile and saving to the file %s' % str(output)) #b3main.terminate() # b3main.kill() # we must end the main function in order for the profiler to output its results (if we didn't launch a thread and just closed the process, it would have done no result) print('=> Profile done ! Exiting...') profiler2 = posh.share(profiler) profiler2.dump_stats(output) #signal.signal(signal.SIGABRT, b3main) raise SystemExit(222) except SystemExit, e: print('SystemExit!') sys.exit(223)
def run_profile(): import profile profile.run('for i in range(10): demo()', '/tmp/profile.out') import pstats p = pstats.Stats('/tmp/profile.out') p.strip_dirs().sort_stats('time', 'cum').print_stats(60) p.strip_dirs().sort_stats('cum', 'time').print_stats(60)
def __profile__(): import profile profile.run('__fulltest__()') #profile.run('__filetest__()')