def profile_lines(self, functions, statement): from line_profiler import LineProfiler import __builtin__ profile = LineProfiler(*functions) # Add the profiler to the builtins for @profile.. # will probably not work for all modules in ecoControl, # as they are already imported before and @profile will then throw an error if 'profile' in __builtin__.__dict__: had_profile = True old_profile = __builtin__.__dict__['profile'] else: had_profile = False old_profile = None __builtin__.__dict__['profile'] = profile try: try: profile.runctx(statement, globals(), locals()) message = '' except SystemExit: message = """*** SystemExit exception caught in code being profiled.""" except KeyboardInterrupt: message = ("*** KeyboardInterrupt exception caught in code being " "profiled.") finally: if had_profile: __builtin__.__dict__['profile'] = old_profile # Trap text output. stdout_trap = StringIO() profile.print_stats(stdout_trap) output = stdout_trap.getvalue() output = output.rstrip() pfile = open("profile.txt", 'a') pfile.write("\n\n" + 20 * "=" + "*********====================== profile at time " + time.strftime("%b %d %Y %H:%M:%S", time.gmtime(time.time())) + "==================================\n\n") pfile.write(output) pfile.close() print '\n*** Profile printout saved to text file profile.txt', message
class ProfilingPanel(Panel): """ Panel that displays profiling information. """ title = _('Profiling') template = 'debug_toolbar_line_profiler/panels/profiling.html' def _unwrap_closure_and_profile(self, func): if not hasattr(func, '__code__'): return self.line_profiler.add_function(func) for subfunc in getattr(func, 'profile_additional', []): self._unwrap_closure_and_profile(subfunc) if func.__closure__: for cell in func.__closure__: target = cell.cell_contents if inspect.isclass(target) and View in inspect.getmro(target): for name, value in inspect.getmembers(target): if name[0] != '_' and inspect.ismethod(value): self._unwrap_closure_and_profile(value) else: self._unwrap_closure_and_profile(target) def process_view(self, request, view_func, view_args, view_kwargs): self.profiler = cProfile.Profile() args = (request,) + view_args self.line_profiler = LineProfiler() self._unwrap_closure_and_profile(view_func) self.line_profiler.enable_by_count() out = self.profiler.runcall(view_func, *args, **view_kwargs) self.line_profiler.disable_by_count() return out def add_node(self, func_list, func, max_depth, cum_time=0.1): func_list.append(func) func.has_subfuncs = False if func.depth < max_depth: for subfunc in func.subfuncs(): if (subfunc.stats[3] >= cum_time or (hasattr(self.stats, 'line_stats') and (subfunc.func in self.stats.line_stats.timings))): func.has_subfuncs = True self.add_node(func_list, subfunc, max_depth, cum_time=cum_time) def process_response(self, request, response): if not hasattr(self, 'profiler'): return None # Could be delayed until the panel content is requested (perf. optim.) self.profiler.create_stats() self.stats = DjangoDebugToolbarStats(self.profiler) self.stats.line_stats = self.line_profiler.get_stats() self.stats.calc_callees() root = FunctionCall(self.stats, self.stats.get_root_func(), depth=0) func_list = [] self.add_node(func_list, root, 10, root.stats[3] / 8) self.record_stats({'func_list': func_list})
def profile_each_line(func, *args, **kwargs): profiler = LineProfiler() profiled_func = profiler(func) retval = None try: retval = profiled_func(*args, **kwargs) finally: profiler.print_stats() return retval
def profiled_func(*args, **kwargs): try: profiler = LineProfiler() profiler.add_function(func) for f in follow: profiler.add_function(f) profiler.enable_by_count() return func(*args, **kwargs) finally: profiler.print_stats()
def wrapper(*args, **kwargs): # Don't profile if debugging is off (PROD server mode) if DEBUG: logger.error('Line Profiling (@profile_this_by_line) ' + func.__name__ + '() to ' + stats_filename + '.') profiler = LineProfiler() profiled_func = profiler(func) try: retval = profiled_func(*args, **kwargs) finally: # profiler.print_stats() profiler.dump_stats(stats_filename) else: logger.error('Line Profiling (@profile_this_by_line) attempted on ' + func.__name__ + '() while in production mode. Profiling Bypassed.') retval = func(*args, **kwargs) return retval
def profiled_func(*args, **kwargs): try: profiler = LineProfiler() profiler.add_function(func) for f in follow: if isinstance(f, basestring): f = to_function(f) profiler.add_function(f) profiler.enable_by_count() return func(*args, **kwargs) finally: profiler.print_stats()
def __init__(self, *args): self.profile = LineProfiler() if len(args) > 0: for func in args: if callable(func): self.add_function(func)
def process_view(self, request, view_func, view_args, view_kwargs): self.profiler = cProfile.Profile() args = (request,) + view_args self.line_profiler = LineProfiler() self._unwrap_closure_and_profile(view_func) self.line_profiler.enable_by_count() out = self.profiler.runcall(view_func, *args, **view_kwargs) self.line_profiler.disable_by_count() return out
def profile(algo, data=None, to_profile=[]): """ Profile algorithm using line_profiler. :param algo: Algorithm instance. :param data: Stock prices, default is random portfolio. :param to_profile: List of methods to profile, default is `step` method. Example of use: tools.profile(Anticor(window=30, c_version=False), to_profile=[Anticor.weights]) """ from line_profiler import LineProfiler if data is None: data = random_portfolio(n=1000, k=10, mu=0.) to_profile = to_profile or [algo.step] profile = LineProfiler(*to_profile) profile.runcall(algo.run, data) profile.print_stats()
def profile_list_deserialization(serializer, child_serializer, data_list): if os.environ.get('CI', None) != 'true' or not LineProfiler: return profile = LineProfiler(serializer.to_internal_value, child_serializer.to_internal_value) profile.enable() serializer.to_internal_value(data_list) profile.disable() profile.print_stats()
def profiled_func(*args, **kwargs): try: lp = LineProfiler() lp.add_function(f) lp.enable_by_count() return f(*args, **kwargs) finally: lp.print_stats()
def profile_list_serialization(serializer, child_serializer, instances_list): if os.environ.get('CI', None) != 'true' or not LineProfiler: return profile = LineProfiler(serializer.instances_list, child_serializer.instances_list) profile.enable() serializer.to_representation(instances_list) profile.disable() profile.print_stats()
def DataReader_bin_test2(self): self.select_file(num=1) prf = LineProfiler() prf.add_function(self.read_bin_file_to_tx2) prf.runcall(self.read_bin_file_to_tx2, start=3 * 10**7, datapoints=10**6) prf.print_stats() print(len(self.x), math.log10((len(self.x)))) self.plot_timecorse_of_move(show_it=1)
def main(): profiler = cProfile.Profile() profiler.enable() function_runner('original_method') function_runner('step_one') function_runner('step_two') function_runner('step_three') function_runner('step_four') function_runner('step_five') function_runner('step_six') function_runner('step_seven') function_runner('step_eight') function_runner('step_nine') function_runner('current') profiler.disable() profiler.dump_stats('function_event.stats') line_profiler = LineProfiler(CurrentFunctionContainer().current) line_profiler.enable() function_runner('current') line_profiler.disable() line_profiler.dump_stats('function_event.line_stats') line_profiler.print_stats() print 'Original', timeit.timeit( lambda: function_runner('original_method'), number=7) print 'One', timeit.timeit( lambda: function_runner('step_one'), number=7) print 'Two', timeit.timeit( lambda: function_runner('step_two'), number=7) print 'Three', timeit.timeit( lambda: function_runner('step_three'), number=7) print 'Four', timeit.timeit( lambda: function_runner('step_four'), number=7) print 'Five', timeit.timeit( lambda: function_runner('step_five'), number=7) print 'Six', timeit.timeit( lambda: function_runner('step_six'), number=7) print 'Seven', timeit.timeit( lambda: function_runner('step_seven'), number=7) print 'Eight', timeit.timeit( lambda: function_runner('step_eight'), number=7) print 'Nine', timeit.timeit( lambda: function_runner('step_nine'), number=7) print 'Current', timeit.timeit( lambda: function_runner('current'), number=7)
def benchmark(cls, func, *args): from line_profiler import LineProfiler prf = LineProfiler() prf.add_function(func) ret = prf.runcall(func, *args) prf.print_stats() return ret
def profile_on(fcn_names=None): if fcn_names and HAS_LINE_PROFILER: pr = LineProfiler() for fcn_name in fcn_names: pr.add_function(fcn_name) pr.enable() return pr pr = cProfile.Profile() pr.enable() return pr
def timetest(func, *para): p = LineProfiler() p.add_function(func) p.enable_by_count() p_wrapper = p(func) p_wrapper(*para) # Printing print(func(*para)) p.print_stats()
def process_view(self, request, view_func, view_args, view_kwargs): __traceback_hide__ = True self.profiler = cProfile.Profile() args = (request,) + view_args if DJ_PROFILE_USE_LINE_PROFILER: self.line_profiler = LineProfiler() self._unwrap_closure_and_profile(view_func) self.line_profiler.enable_by_count() out = self.profiler.runcall(view_func, *args, **view_kwargs) self.line_profiler.disable_by_count() else: self.line_profiler = None out = self.profiler.runcall(view_func, *args, **view_kwargs) return out
def setup(self): if self.mode == 'c': import cProfile self.profiler = cProfile.Profile() elif self.mode == 'l': from line_profiler import LineProfiler self.profiler = LineProfiler() elif self.mode == 'h': import hotshot self.info['name'] = 'special.prof' self.profiler = hotshot.Profile(self.info['name']) self.test.setup()
def process_view(self, request, view_func, view_args, view_kwargs): self.profiler = cProfile.Profile() args = (request,) + view_args self.line_profiler = LineProfiler() self._unwrap_closure_and_profile(view_func) if view_func.func_globals['__name__'] == 'django.views.generic.base': for cell in view_func.func_closure: target = cell.cell_contents if inspect.isclass(target) and View in inspect.getmro(target): for name, value in inspect.getmembers(target): if name[0] != '_' and inspect.ismethod(value): self._unwrap_closure_and_profile(value) self.line_profiler.enable_by_count() out = self.profiler.runcall(view_func, *args, **view_kwargs) self.line_profiler.disable_by_count() return out
class Profiler(object): def __init__(self, *args): self.profile = LineProfiler() if len(args) > 0: for func in args: if callable(func): self.add_function(func) def add_function(self, func): self.profile.add_function(func) def __enter__(self): self.profile.enable_by_count() def __exit__(self, type, value, traceback): self.profile.disable_by_count() self.profile.print_stats()
def speedtest_validate_transaction(): # create a transaction b = bigchaindb.Bigchain() tx = b.create_transaction(b.me, b.me, None, 'CREATE') tx_signed = b.sign_transaction(tx, b.me_private) # setup the profiler profiler = LineProfiler() profiler.enable_by_count() profiler.add_function(bigchaindb.Bigchain.validate_transaction) # validate_transaction 1000 times for i in range(1000): b.validate_transaction(tx_signed) profiler.print_stats()
def _transform_STRIKED(self, match: Match) -> str: if not match['STRIKED_TEXT']: return '' return f"<s>{self._parse(match['STRIKED_TEXT'], 'STRIKED')}</s>" def _transform_SUPERSCRIPT(self, match: Match) -> str: if not match['SUPERSCRIPT_TEXT']: return '' return f"<sup>{self._parse(match['SUPERSCRIPT_TEXT'], 'SUPERSCRIPT')}</sup>" def _transform_SUBSCRIPT(self, match: Match) -> str: if not match['SUBSCRIPT_TEXT']: return '' return f"<sub>{self._parse(match['SUBSCRIPT_TEXT'], 'SUBSCRIPT')}</sub>" def _transform_HORIZ_RULE(self, match: Match) -> str: return f'<hr />' def _post_BACKSLASH_UNESCAPE(self, text: str) -> str: return self._backslash_escape_re.sub(r'\1', text) d = DefaultRenderer() if __name__ == '__main__': from line_profiler import LineProfiler lp = LineProfiler() lp.add_function(d._parse) lp.runcall(d.parse, '*****' * 2000) lp.print_stats() print(d.parse('**__hi__**'))
#!/usr/bin/env python import sys from line_profiler import LineProfiler if __name__ == '__main__': filename = sys.argv[1] profiler = LineProfiler() profiler.runctx( 'execfile(%r, globals())' % (filename,), locals(), locals()) profiler.print_stats()
if stats[l][j]['case'] == 'L' and stats[k][i]['case'] == 'U': continue if stats[l][j]['alph'] == 'A' and stats[k][i]['alph'] == 'N': continue if stats[l][j]['alph'] == 'N' and stats[k][i]['alph'] == 'A': continue y = tabs[l][j] if len(y) < 3: continue # avoid boolean # common types comtyp = typsets[k][i] & typsets[l][j] # if len(comtyp) == 0: continue # not good, miss most refs # if y <= x: # (strict) subset # instead: 90% subset fracsubs = fracsubset(y, x) if fracsubs >= 0.9: # table l, column j is subset of table k, column i: table l references table k ? fout.write("insert into sub (l, j, k, i, comtyp, fracsubset, nchild, nparent, fnchild, fnparent)" + " values (%d, %d, %d, %d, '%s', %f, %d, %d, '%s', '%s');\n" % (l, j, k, i, len(comtyp), fracsubs, len(y), len(x), cleanfn(sam[l]), cleanfn(sam[k]))) fout.write("commit;\n") fout.close() t3 = time() print('references done in %.1f seconds' % (t3-t2,), file=sys.stderr) print('total run time %.1f seconds or %.1f minutes' % (t3-t0, (t3-t0)/60), file=sys.stderr) sys.exit(0) if '--profile' in sys.argv: print('profiling..', file=sys.stderr) prof = LineProfiler(main) prof.run('main()') prof.print_stats() else: main()
def main(): profiler = LineProfiler() for f in funcs_to_profile: profiler.add_function(f) profiler.wrap_function(run)() profiler.print_stats(stripzeros=True)
""" Line-by-line profiling of CPU use of Python scripts in :mod:`mandelbrot.implementations` using :mod:`line_profiler`. """ from line_profiler import LineProfiler import mandelbrot if __name__ == "__main__": args = mandelbrot.parsed_mandelbrot_args() for impl in mandelbrot.all_implementations(): print(f"About to profile {impl.id_}") profile = LineProfiler() profile.add_function(impl.callable) cmd = ( f"{impl.fully_qualified_name}(grid_side_size={args.grid_side_size}, " f"max_iter={args.max_iter})" ) profile.run(cmd) profile.print_stats()
pr.disable() sortby = 'cumulative' ps = pstats.Stats(pr).sort_stats(sortby) ps.print_stats() """ L = 200 v = 0.1 d = 0.05 e = 0.05 seed1 = 1 seed2 = 2 seed3 = 3 ref_period = 51 tot_time = 10**6 pace_rate = 220 lp = LineProfiler() lp_wrapper = lp(CMP2D) lp.add_function(CMP2d_1step) lp.add_function(SinusRhythm) lp.add_function(Excite) lp.add_function(Relaxing) lp_wrapper(L, v, d, seed2, tot_time, pace_rate, ref_period) lp.print_stats() """ # Runs animation Neigh_u, Neigh_d, Neigh_r, Neigh_l, Phases, Funct = CreateAtrium(L,v,d,seed2,ref_period) fig = plt.figure(figsize = [10,10]) ax = plt.subplot() ax.set_axis_off() mat = ax.matshow(Phases.reshape([L,L]),cmap=plt.cm.gray_r)
class SpecialTestRunner(SpecialTest): """ Test runner, calls the specified test under specified profiler Mode = None - no profiler, "c" - cProfile, "l" - LineProfiler, "h" - hotshot """ def __init__(self, test, mode=None): super(SpecialTestRunner, self).__init__() self.mode = mode self.test = test self.profiler = None def setup(self): if self.mode == 'c': import cProfile self.profiler = cProfile.Profile() elif self.mode == 'l': from line_profiler import LineProfiler self.profiler = LineProfiler() elif self.mode == 'h': import hotshot self.info['name'] = 'special.prof' self.profiler = hotshot.Profile(self.info['name']) self.test.setup() def run(self): if self.mode == 'c': self.profiler.enable() elif self.mode == 'l': self.profiler.enable_by_count() self.profiler.add_function(Handler.handle) self.profiler.add_function(Condition.check_string_match) self.profiler.add_function(Condition.check_function) self.profiler.add_function(Condition.check_list) t = Timer() # Run itself if self.mode == 'h': self.profiler.runcall(self.test.run) else: self.test.run() print('Test time: %s' % t.delta()) if self.mode == 'c': import pstats import StringIO self.profiler.disable() sio = StringIO.StringIO() ps = pstats.Stats(self.profiler, stream=sio).sort_stats('time') ps.print_stats() print(sio.getvalue()) elif self.mode == 'h': import hotshot.stats print('Processing results...') self.profiler.close() name = self.info['name'] stats = hotshot.stats.load(name) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(50) print( 'Run "hotshot2calltree -o %s.out %s" to generate the cachegrind file' % (name, name)) elif self.mode == 'l': self.profiler.disable() self.profiler.print_stats()
from line_profiler import LineProfiler from pyqtgraph.Qt import QtCore, QtGui import pandas as pd import stockGUIs as tgui import sys sys.path.append("..") from twine_theory.domain import twine_theory as tt df = pd.read_csv("000001.csv") df.sort_values('date') app = QtGui.QApplication(sys.argv) w = QtGui.QMainWindow() w.show() w.resize(800, 600) profiler = LineProfiler() # import pdb;pdb.set_trace() s = tgui.StickWidget(tt.KSeq('day', [])) lp_wrapper = profiler(tgui.StickWidget.__init__) lp_wrapper(s, tt.KSeq('day', df)) profiler.print_stats()
def __call__(self, *args, **kwargs): result = None if logger.getEffectiveLevel() == logging.MPROFILE: import sys result = self.function(*args, **kwargs) logger.debug(">>Tracing memory size : return object is %s bytes", sys.getsizeof(result)) elif logger.getEffectiveLevel() == logging.CPROFILE: import cProfile profile = cProfile.Profile() try: profile.enable() result = self.function(*args, **kwargs) profile.disable() finally: profile.print_stats() elif logger.getEffectiveLevel() == logging.LPROFILE: try: from line_profiler import LineProfiler try: profiler = LineProfiler() profiler.add_function(self.function) profiler.enable_by_count() result = self.function(*args, **kwargs) finally: profiler.print_stats() except ImportError: logger.debug("Error importing line_profiler.") return self.function(*args, **kwargs) elif logger.getEffectiveLevel() == logging.MEMPROFILE: try: from memory_profiler import LineProfiler, show_results try: profiler = LineProfiler() profiler.add_function(self.function) profiler.enable_by_count() result = self.function(*args, **kwargs) finally: show_results(profiler) except ImportError: logger.debug("Error importing memory_profiler.") result = self.function(*args, **kwargs) else: result = self.function(*args, **kwargs) return result
result = np.empty((2, len(result_index))) lib.take_1d(av, aindexer, result[0]) lib.take_1d(bv, bindexer, result[1]) return result_index, result def do_inner_join(a, b, av, bv): result_index, aindexer, bindexer = lib.inner_join_indexer(a, b) result = np.empty((2, len(result_index))) lib.take_1d(av, aindexer, result[0]) lib.take_1d(bv, bindexer, result[1]) return result_index, result from line_profiler import LineProfiler prof = LineProfiler() from pandas.util.testing import set_trace def do_left_join_python(a, b, av, bv): indexer, mask = lib.ordered_left_join_int64(a, b) n, ak = av.shape _, bk = bv.shape result_width = ak + bk result = np.empty((result_width, n), dtype=np.float64) result[:ak] = av.T bchunk = result[ak:]
def run_profiling(args): lprofiler = LineProfiler() monitor_fuctions = [ api.problem.submit_key, api.problem.get_unlocked_pids, api.problem.get_solved_pids, api.problem.get_all_problems, api.problem.get_solved_problems, api.stats.get_score, api.cache.memoize, api.autogen.grade_problem_instance, api.autogen.get_problem_instance, api.autogen.get_number_of_instances ] for func in monitor_fuctions: lprofiler.add_function(func) lprofiler.enable() if args.stack: profiler = Profiler(use_signal=False) profiler.start() for func, a, kw in operations: func(*a, **kw) if args.stack: profiler.stop() lprofiler.disable() if args.print: print(profiler.output_text(unicode=True, color=True)) lprofiler.print_stats() output = open(args.output, "w") if args.stack: output.write(profiler.output_text(unicode=True)) if args.output_html is not None: output_html = open(args.output_html, "w") output_html.write(profiler.output_html()) output_html.close() print("Wrote test info to " + args.output_html) lprofiler.print_stats(output) output.close() print("Wrote test info to " + args.output)
"""Mongo DB class to import the CSV data and to display the data""" import csv import os import time import atexit import multiprocessing from multiprocessing.pool import Pool import pymongo from pymongo import MongoClient from line_profiler import LineProfiler profile = LineProfiler() atexit.register(profile.print_stats) # acquire_lock = threading.Lock() class MongoDBConnection: """MongoDB Connection""" def __init__(self, host='localhost', port=27017): """ be sure to use the ip address not name for local windows""" self.host = host self.port = port self.connection = None def __enter__(self): self.connection = MongoClient(self.host, self.port) return self def __exit__(self, exc_type, exc_val, exc_tb): self.connection.close()
def process_view(self, request, view_func, view_args, view_kwargs): request.devserver_profiler = LineProfiler() request.devserver_profiler_run = False if (DEVSERVER_AUTO_PROFILE): _unwrap_closure_and_profile(request.devserver_profiler, view_func) request.devserver_profiler.enable_by_count()
try: m = DFMessage(fmt, elements, False) except ValueError: return self._parse_next() self._add_msg(m) return m if __name__ == "__main__": import sys use_profiler = False if use_profiler: from line_profiler import LineProfiler profiler = LineProfiler() profiler.add_function(DFReader_binary._parse_next) profiler.add_function(DFReader_binary._add_msg) profiler.add_function(DFReader._set_time) profiler.enable_by_count() filename = sys.argv[1] if filename.endswith('.log'): log = DFReader_text(filename) else: log = DFReader_binary(filename) while True: m = log.recv_msg() if m is None: break #print(m)
class ProfilingDebugPanel(DebugPanel): """ Panel that displays the Django version. """ name = 'Profiling' template = 'debug_toolbar/panels/profiling.html' has_content = True def nav_title(self): return _('Profiling') def url(self): return '' def title(self): return _('Profiling') def _unwrap_closure_and_profile(self, func): if not hasattr(func, 'func_code'): return self.line_profiler.add_function(func) if func.func_closure: for cell in func.func_closure: if hasattr(cell.cell_contents, 'func_code'): self._unwrap_closure_and_profile(cell.cell_contents) def process_view(self, request, view_func, view_args, view_kwargs): __traceback_hide__ = True self.profiler = cProfile.Profile() args = (request,) + view_args if DJ_PROFILE_USE_LINE_PROFILER: self.line_profiler = LineProfiler() self._unwrap_closure_and_profile(view_func) self.line_profiler.enable_by_count() out = self.profiler.runcall(view_func, *args, **view_kwargs) self.line_profiler.disable_by_count() else: self.line_profiler = None out = self.profiler.runcall(view_func, *args, **view_kwargs) return out def add_node(self, func_list, func, max_depth, cum_time=0.1): func_list.append(func) func.has_subfuncs = False if func.depth < max_depth: for subfunc in func.subfuncs(): if (subfunc.stats[3] >= cum_time or (hasattr(self.stats, 'line_stats') and (subfunc.func in self.stats.line_stats.timings))): func.has_subfuncs = True self.add_node(func_list, subfunc, max_depth, cum_time=cum_time) def process_response(self, request, response): self.profiler.create_stats() self.stats = DjangoDebugToolbarStats(self.profiler) if DJ_PROFILE_USE_LINE_PROFILER: self.stats.line_stats = self.line_profiler.get_stats() self.stats.calc_callees() root = FunctionCall(self.stats, self.stats.get_root_func(), depth=0) func_list = [] self.add_node(func_list, root, 10, root.stats[3]/8) self.stats_record({'func_list': func_list})
def test_enable_disable(self): lp = LineProfiler() self.assertEqual(lp.enable_count, 0) lp.enable_by_count() self.assertEqual(lp.enable_count, 1) lp.enable_by_count() self.assertEqual(lp.enable_count, 2) lp.disable_by_count() self.assertEqual(lp.enable_count, 1) lp.disable_by_count() self.assertEqual(lp.enable_count, 0) self.assertEqual(lp.last_time, {}) lp.disable_by_count() self.assertEqual(lp.enable_count, 0) with lp: self.assertEqual(lp.enable_count, 1) with lp: self.assertEqual(lp.enable_count, 2) self.assertEqual(lp.enable_count, 1) self.assertEqual(lp.enable_count, 0) self.assertEqual(lp.last_time, {}) with self.assertRaises(RuntimeError): self.assertEqual(lp.enable_count, 0) with lp: self.assertEqual(lp.enable_count, 1) raise RuntimeError() self.assertEqual(lp.enable_count, 0) self.assertEqual(lp.last_time, {})
# create the fasttext encoder model enc = FTEncoder(preprocessor=prep.preprocess_text, data_dir='TestData/', model='Models/test_model.bin') # get an encoding print(enc.get_encoding("The blue bat hit the red ball")) # get a series of encodings as a datafrmae text_ser = pd.Series([ "The steel ball struck the glass table and it shattered", "The glass table was struck by the steel ball and it shattered", "The glass table, struck by the steel ball, shattered." ]) print(enc.get_encodings_for_series(text_ser)) if __name__ == "__main__": from line_profiler import LineProfiler from Preproc import Preproc # setup line profiling profiler = LineProfiler() profiler.add_function(Preproc.preprocess_text) test_func = profiler(loading_and_using_model) # call the test function and print profiling results test_func() profiler.print_stats()
except: pass # print data.iloc[0:num, i] frame = pd.DataFrame.from_items(d, orient="index", columns=["Median (0.25)", "Error (%)", "Median (0.05)", "Error (%)", "Truth"]) print(frame.to_latex(float_format=(lambda x: u"%1.1f" % x))) if args.time: test_median() if args.size: size_vs_error() if args.quality: no_test_DP_median() if args.cprof: import cProfile cProfile.run("test_median()", sort="tottime") if args.lprof: from line_profiler import LineProfiler profile = LineProfiler(test_median, CountSketchCt.estimate, CountSketchCt.aggregate, Ct.__add__, EcPt.__add__, EcPt.__neg__, EcPt.__copy__,) profile.run("test_median()") profile.print_stats()
pace = 220 #RiskCurve(L,d,e,rp,pace,time) """ pr = cProfile.Profile() pr.enable() t_AF = CMP2D(seed1,seed2,seed3,L,v,d,e,rp,pace,time) print('Atrium') print(t_AF) pr.disable() sortby = 'cumulative' ps = pstats.Stats(pr).sort_stats(sortby) ps.print_stats() """ #CMP2D(seed1,seed2,seed3,L,v,d,e,rp,pace,time) lp = LineProfiler() lp_wrapper = lp(CMP2D) #lp.add_function(CMP2d_1step) lp.add_function(SinusRhythm) #lp.add_function(Conduct) #lp.add_function(Relaxing) lp_wrapper(seed1,seed2,seed3,L,v,d,e,rp,pace,time) lp.print_stats() """ L = 200 v = 0.1 d = 0.05 e = 0.05 seed1 = 1 seed2 = 2 seed3 = 3
from pagnn import settings from pagnn.training.dcn.main import main if settings.PROFILER == "cProfile": import cProfile sys.exit(cProfile.run("main()", filename="parent.prof")) elif settings.PROFILER == "line_profiler": from line_profiler import LineProfiler from pagnn.datavargan import dataset_to_datavar, gen_adj_pool, push_adjs, push_seqs from pagnn.io import _read_random_row_group, iter_datarows from pagnn.training.dcn.main import train from pagnn.training.dcn.utils import basic_permuted_sequence_adder, generate_batch lp = LineProfiler() lp.add_function(dataset_to_datavar) lp.add_function(gen_adj_pool) lp.add_function(push_seqs) lp.add_function(push_adjs) lp.add_function(iter_datarows) lp.add_function(train) lp.add_function(basic_permuted_sequence_adder) lp.add_function(generate_batch) lp.add_function(_read_random_row_group) # Profile the main function lp_wrapper = lp(main) lp_wrapper() # Print results
default save file is 'default_stats.pkl', replace it with your likes we can use this script for display stats files. `python benchmark/tools/profiler.py -f default_stats.pkl` """ import argparse import os import pickle import sys from time import sleep try: from line_profiler import LineProfiler, show_text PROFILER = LineProfiler() def do_profile( follow=[], profiler=None, # pylint: disable=W0102 stats_file="default_stats.pkl"): """Warp the profile function into decorator.""" def inner(func): def profiled_func(*args, **kwargs): try: profiler.add_function(func) for sub_func in follow: profiler.add_function(sub_func) profiler.enable_by_count() return func(*args, **kwargs) finally:
import base64 import cv2 import numpy as np from turbojpeg import TurboJPEG import sys from line_profiler import LineProfiler profile = LineProfiler() @profile def compress_encode_image(image: np.ndarray, quality: int = 30) -> str: """ Compress numpy array image and encode the compressed numpy array into byte string """ encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), quality] ret, jpg = cv2.imencode(".jpg", image, encode_param) if ret: return base64.b64encode(jpg).decode("ascii") raise Exception("Failed to compress image") @profile def decode_decompress_image(image: str) -> np.ndarray: """ decode a byte string into numpy array and decompress it into an image """ compressed = np.frombuffer(base64.b64decode(image), dtype=np.uint8) return cv2.imdecode(compressed, 1) @profile def main():
break if p == -1: print("NA") else: print(p) def main(): N = 1000001 temp = [True] * (N + 1) temp[0] = temp[1] = False for i in range(2, int((N + 1)**0.5) + 1): if temp[i]: temp[i + i::i] = [False] * (len(temp[i + i::i])) while True: n, price = map(int, input().split()) if n == 0 and price == 0: return ps = [] for i in range(n): ps.append(int(input())) solve(price, ps, temp) prf = LineProfiler() prf.add_function(main) prf.runcall(main) prf.print_stats() main()
from twilio.rest import Client from line_profiler import LineProfiler send_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 短信收发费用0.028$ def send_message(): account_sid = 'ACd615e71b7ad5f4686943f7e716e95b0f' auth_token = 'a40401a59745b0c7e3e0202ef9dc3837' client = Client(account_sid, auth_token) message = client.messages \ .create( body="Join Earth's mightiest heroes. Like Kevin Bacon.", from_='+12187890888', to='+8613871115289' ) print('发送时间:%s \n状态:发送成功!' % send_time) print('接收短信号码:' + message.to) print('短信内容:\n' + message.body) # 打印短信内容 print('短信SID:' + message.sid) # 打印SID #send_message() # 调用执行函数 lptest = LineProfiler(send_message) lptest.run('send_message()') lptest.print_stats()
(1) 1부터 N까지의 정수 중에서 소수를 찾는다 (2) 1부터 N까지의 정수의 합을 계산한다 """ # (1) out = [] append = out.append for k in range(1, n+1): if is_prime(k): append(k) # (2) a = mysum(n) return [out, a] def task2(n): """ 1부터 N까지의 sqrt()를 계산한다 """ return np.sqrt(np.arange(1, n+1)) def main(): task1(100000) # 부하가 큰 계산 task2(100000) # 부하가 작은 계산 if __name__ == '__main__': # (3) from line_profiler import LineProfiler prf = LineProfiler() prf.add_function(is_prime) prf.runcall(is_prime, 999) prf.print_stats()
class ProfilingDebugPanel(DebugPanel): """ Panel that displays the Django version. """ name = 'Profiling' template = 'debug_toolbar/panels/profiling.html' has_content = True def nav_title(self): return _('Profiling') def url(self): return '' def title(self): return _('Profiling') def _unwrap_closure_and_profile(self, func): if not hasattr(func, 'func_code'): return self.line_profiler.add_function(func) if func.func_closure: for cell in func.func_closure: if hasattr(cell.cell_contents, 'func_code'): self._unwrap_closure_and_profile(cell.cell_contents) def process_view(self, request, view_func, view_args, view_kwargs): __traceback_hide__ = True self.profiler = cProfile.Profile() args = (request, ) + view_args if DJ_PROFILE_USE_LINE_PROFILER: self.line_profiler = LineProfiler() self._unwrap_closure_and_profile(view_func) self.line_profiler.enable_by_count() out = self.profiler.runcall(view_func, *args, **view_kwargs) self.line_profiler.disable_by_count() else: self.line_profiler = None out = self.profiler.runcall(view_func, *args, **view_kwargs) return out def add_node(self, func_list, func, max_depth, cum_time=0.1): func_list.append(func) func.has_subfuncs = False if func.depth < max_depth: for subfunc in func.subfuncs(): if (subfunc.stats[3] >= cum_time or (hasattr(self.stats, 'line_stats') and (subfunc.func in self.stats.line_stats.timings))): func.has_subfuncs = True self.add_node(func_list, subfunc, max_depth, cum_time=cum_time) def process_response(self, request, response): __traceback_hide__ = True if not hasattr(self, 'profiler'): return None self.profiler.create_stats() self.stats = DjangoDebugToolbarStats(self.profiler) if DJ_PROFILE_USE_LINE_PROFILER: self.stats.line_stats = self.line_profiler.get_stats() self.stats.calc_callees() root = FunctionCall(self.stats, self.stats.get_root_func(), depth=0) func_list = [] self.add_node(func_list, root, 10, root.stats[3] / 8) self.record_stats({'func_list': func_list})
from line_profiler import LineProfiler from simulation import simulate_timesteps from helpers import record_timestep_data lp = LineProfiler() lp.add_function(record_timestep_data) lp_wrapper = lp(simulate_timesteps) lp_wrapper(new_simulation) lp.print_stats()
import numpy as np from line_profiler import LineProfiler def euclidean_broadcast(x, y): """Euclidean square distance matrix. Inputs: x: (N, m) numpy array y: (N, m) numpy array Ouput: (N, N) Euclidean square distance matrix: r_ij = (x_ij - y_ij)^2 """ diff = x[:, np.newaxis, :] - y[np.newaxis, :, :] return (diff * diff).sum(axis=2) if __name__ == "__main__": nsamples = 2000 nfeat = 50 x = 10. * np.random.random([nsamples, nfeat]) lp = LineProfiler() lp(euclidean_broadcast)(x, x) lp.print_stats()
class ProfilingPanel(Panel): """ Panel that displays profiling information. """ title = _('Profiling') template = 'debug_toolbar_line_profiler/panels/profiling.html' def _unwrap_closure_and_profile(self, func): if not hasattr(func, '__code__'): return self.line_profiler.add_function(func) for subfunc in getattr(func, 'profile_additional', []): self._unwrap_closure_and_profile(subfunc) if PY2: func_closure = func.func_closure else: func_closure = func.__closure__ if func_closure: for cell in func_closure: target = cell.cell_contents if hasattr(target, '__code__'): self._unwrap_closure_and_profile(cell.cell_contents) if inspect.isclass(target) and View in inspect.getmro(target): for name, value in inspect.getmembers(target): if name[0] != '_' and inspect.ismethod(value): self._unwrap_closure_and_profile(value) def process_view(self, request, view_func, view_args, view_kwargs): self.view_func = view_func self.profiler = cProfile.Profile() args = (request,) + view_args self.line_profiler = LineProfiler() self._unwrap_closure_and_profile(view_func) signals.profiler_setup.send(sender=self, profiler=self.line_profiler, view_func=view_func, view_args=view_args, view_kwargs=view_kwargs) self.line_profiler.enable_by_count() out = self.profiler.runcall(view_func, *args, **view_kwargs) self.line_profiler.disable_by_count() return out def add_node(self, func_list, func, max_depth, cum_time=0.1): """ add_node does a depth first traversal of the call graph, appending a FunctionCall object to func_list, so that the Django template only has to do a single for loop over func_list that can render a tree structure Parameters: func_list is an array that will have a FunctionCall for each call added to it func is a FunctionCall object that will have all its callees added max_depth is the maximum depth we should recurse cum_time is the minimum cum_time a function should have to be included in the output """ func_list.append(func) func.has_subfuncs = False # this function somewhat dangerously relies on FunctionCall to set its # subfuncs' depth argument correctly if func.depth >= max_depth: return # func.subfuncs returns FunctionCall objects subs = sorted(func.subfuncs(), key=FunctionCall.cumtime, reverse=True) for subfunc in subs: # a sub function is important if it takes a long time or it has # line_stats if (subfunc.cumtime() >= cum_time or (hasattr(self.stats, 'line_stats') and subfunc.func in self.stats.line_stats.timings)): func.has_subfuncs = True self.add_node( func_list=func_list, func=subfunc, max_depth=max_depth, cum_time=subfunc.cumtime()/16) def process_response(self, request, response): if not hasattr(self, 'profiler'): return None # Could be delayed until the panel content is requested (perf. optim.) self.profiler.create_stats() self.stats = DjangoDebugToolbarStats(self.profiler) self.stats.line_stats = self.line_profiler.get_stats() self.stats.calc_callees() func_list = [] root_func = self.stats.get_root_func(self.view_func) if root_func is not None: root_node = FunctionCall(statobj=self.stats, func=root_func, depth=0) self.add_node( func_list=func_list, func=root_node, max_depth=10, cum_time=root_node.cumtime() / 8 ) # else: # what should we do if we didn't detect a root function? It's not # clear what causes this, but there are real world examples of it (see # https://github.com/dmclain/django-debug-toolbar-line-profiler/issues/11) self.record_stats({'func_list': func_list})
from line_profiler import LineProfiler import random def do_other_stuff(numbers): s = sum(numbers) def do_stuff(numbers): do_other_stuff(numbers) l = [numbers[i] / 43 for i in range(len(numbers))] m = ['hello' + str(numbers[i]) for i in range(len(numbers))] if __name__ == '__main__': numbers = [random.randint(1, 100) for i in range(1000)] lp = LineProfiler() lp.add_function(do_other_stuff) # add additional function to profile lp_wrapper = lp(do_stuff) lp_wrapper(numbers) lp.print_stats()
from crowddynamics.examples.collective_motion import FourExits from crowddynamics.logging import setup_logging from crowddynamics.simulation.agents import ThreeCircle, Circular # TODO: https://nvbn.github.io/2017/05/29/complexity/ def main(simulation, iterations: int, **kwargs): hallway = simulation(**kwargs) hallway.update() for i in range(iterations - 1): hallway.update() if __name__ == '__main__': setup_logging() kw = dict(simulation=FourExits, iterations=100, size_active=10, size_herding=190, agent_type=Circular) profiler = LineProfiler(main) profiler.runcall(main, **kw) profiler.print_stats() # prof = memory_profiler.LineProfiler(backend='psutil') # prof(main)(**kw) # memory_profiler.show_results(prof, precision=1)
class ProfilingPanel(Panel): """ Panel that displays profiling information. """ title = _('Profiling') template = 'debug_toolbar_line_profiler/panels/profiling.html' def _unwrap_closure_and_profile(self, func): if not hasattr(func, '__code__'): return self.line_profiler.add_function(func) for subfunc in getattr(func, 'profile_additional', []): self._unwrap_closure_and_profile(subfunc) if PY2: func_closure = func.func_closure else: func_closure = func.__closure__ if func_closure: for cell in func_closure: target = cell.cell_contents if hasattr(target, '__code__'): self._unwrap_closure_and_profile(cell.cell_contents) if inspect.isclass(target) and View in inspect.getmro(target): for name, value in inspect.getmembers(target): if not name.startswith('__') and ( inspect.ismethod(value) or inspect.isfunction(value)): self._unwrap_closure_and_profile(value) def process_request(self, request): match = resolve(request.path) view_func, view_args, view_kwargs = match self.view_func = view_func self.profiler = cProfile.Profile() self.line_profiler = LineProfiler() self._unwrap_closure_and_profile(self.view_func) signals.profiler_setup.send(sender=self, profiler=self.line_profiler, view_func=view_func, view_args=view_args, view_kwargs=view_kwargs) self.line_profiler.enable_by_count() out = self.profiler.runcall(super().process_request, request) self.line_profiler.disable_by_count() return out def add_node(self, func_list, func, max_depth, cum_time=0.1): """ add_node does a depth first traversal of the call graph, appending a FunctionCall object to func_list, so that the Django template only has to do a single for loop over func_list that can render a tree structure Parameters: func_list is an array that will have a FunctionCall for each call added to it func is a FunctionCall object that will have all its callees added max_depth is the maximum depth we should recurse cum_time is the minimum cum_time a function should have to be included in the output """ func_list.append(func) func.has_subfuncs = False # this function somewhat dangerously relies on FunctionCall to set its # subfuncs' depth argument correctly if func.depth >= max_depth: return # func.subfuncs returns FunctionCall objects subs = sorted(func.subfuncs(), key=FunctionCall.cumtime, reverse=True) for subfunc in subs: # a sub function is important if it takes a long time or it has # line_stats if (subfunc.cumtime() >= cum_time or (hasattr(self.stats, 'line_stats') and subfunc.func in self.stats.line_stats.timings)): func.has_subfuncs = True self.add_node(func_list=func_list, func=subfunc, max_depth=max_depth, cum_time=subfunc.cumtime() / 16) def generate_stats(self, request, response): if not hasattr(self, 'profiler'): return None # Could be delayed until the panel content is requested (perf. optim.) self.profiler.create_stats() self.stats = DjangoDebugToolbarStats(self.profiler) self.stats.line_stats = self.line_profiler.get_stats() self.stats.calc_callees() func_list = [] root_func = self.stats.get_root_func(self.view_func) if root_func is not None: root_node = FunctionCall(statobj=self.stats, func=root_func, depth=0) self.add_node(func_list=func_list, func=root_node, max_depth=10, cum_time=root_node.cumtime() / 8) # else: # what should we do if we didn't detect a root function? It's not # clear what causes this, but there are real world examples of it (see # https://github.com/dmclain/django-debug-toolbar-line-profiler/issues/11) self.record_stats({'func_list': func_list})
parser.add_argument('--plot', action='store_true', help='Upload time plot to plotly') args = parser.parse_args() if args.time: xxx = msg_mass() test_full_client(xxx) if args.cprof: import cProfile xxx = msg_mass() cProfile.run("test_full_client(xxx)", sort="tottime") if args.lprof: from line_profiler import LineProfiler #import rscoin.rscservice profile = LineProfiler(rscoin.rscservice.RSCProtocol.handle_Query, rscoin.rscservice.RSCFactory.process_TxQuery, rscoin.Tx.check_transaction, rscoin.Tx.check_transaction_utxo, rscoin.Tx.parse) xxx = msg_mass() profile.run("test_full_client(xxx)") profile.print_stats()
action='store_true', help='Upload time plot to plotly') args = parser.parse_args() if args.time: notest_timing(31) if args.cprof: import cProfile cProfile.run("notest_timing(51)", sort="tottime") if args.lprof: from line_profiler import LineProfiler profile = LineProfiler(VerifyOneOfN, ProveOneOfN, Bn.__init__, Bn.__del__) profile.run("notest_timing(31)") profile.print_stats() if args.plot: all_sizes, prove_time, verify_time = notest_timing() import plotly.plotly as py from plotly.graph_objs import * trace0 = Scatter( x=all_sizes, y=prove_time, name='Proving', )
import numpy as np import random np.random.seed(1729) random.seed(1729) nnodes = 15 g = cd.rand.rand_weights(cd.rand.directed_erdos(nnodes, 3 / (nnodes - 1), 1)) iv_node = random nsamples = 100 samples = { frozenset(): g.sample(nsamples), frozenset({iv_node}): g.sample_interventional_perfect({iv_node: cd.GaussIntervention(1, .1)}, nsamples) } corr = np.corrcoef(samples[frozenset()], rowvar=False) suffstat = dict(C=corr, n=nsamples) profiler = LineProfiler() def run_gsp(): for i in range(20): gsp(suffstat, nnodes, gauss_ci_test, nruns=10) profiler.add_function(gsp) profiler.runcall(run_gsp) profiler.print_stats()