def line_profile(items): """A context manager which prints a line-by-line profile for the given functions, modules, or module names while execution is in its context. Example: with line_profile(__name__, Class.some_function, some_module): do_something() """ from line_profiler import LineProfiler prof = LineProfiler() for item in items: if inspect.isfunction(item): prof.add_function(item) elif inspect.ismodule(item): prof.add_module(item) elif isinstance(item, str): prof.add_module(sys.modules[str]) else: raise TypeError( 'Inputs must be functions, modules, or module names') prof.enable() yield prof.disable() prof.print_stats()
def profiled_func(*args, **kwargs): try: profiler = LineProfiler() profiler.add_function(func) for f in follow: profiler.add_module(f) profiler.enable_by_count() return func(*args, **kwargs) finally: profiler.print_stats()
import app from line_profiler import LineProfiler import numpy as np import time from tqdm import tqdm import matplotlib.pyplot as plt profile = LineProfiler() profile.add_module(app) profile.run("app.Application()") profile.print_stats()
profiler.add_function(func) profiler.enable_by_count() return func(*args, **kwargs) finally: profiler.print_stats() return profiled_func except ImportError: def line_profile(func): def nothing(*args, **kwargs): return func(*args, **kwargs) return nothing # # Module Profiling # if False: from line_profiler import LineProfiler lpr = LineProfiler() # change this to the target file / module to profile import lighthouse.metadata as metadata_module lpr.add_module(metadata_module) # put this code somewhere to dump results: #global lpr #lpr.enable_by_count() #lpr.disable_by_count() #lpr.print_stats()
#!/usr/bin/env python # -*- coding: utf-8 -*- from application import Application from line_profiler import LineProfiler from cell import * from cell_manager import * def main(): app = Application() app.initialize() for y in range(20): for x in range(20): app.cellManager.set_alive(x, y, True) app.cellManager.next_generation() app.window.quit() profile = LineProfiler() profile.add_module(Cell) profile.add_module(CellManager) profile.add_function(main) profile.runcall(main) profile.print_stats()
profiler.print_stats() return profiled_func except ImportError: def line_profile(func): def nothing(*args, **kwargs): return func(*args, **kwargs) return nothing #------------------------------------------------------------------------------ # Module Line Profiling #------------------------------------------------------------------------------ if False: from line_profiler import LineProfiler lpr = LineProfiler() # change this to the target file / module to profile import lighthouse.metadata as metadata lpr.add_module(metadata) # put this code somewhere to dump results: #global lpr #lpr.enable_by_count() #lpr.disable_by_count() #lpr.print_stats(stripzeros=True)