当前位置: 首页>>代码示例>>Python>>正文


Python line_profiler.LineProfiler类代码示例

本文整理汇总了Python中line_profiler.LineProfiler的典型用法代码示例。如果您正苦于以下问题:Python LineProfiler类的具体用法?Python LineProfiler怎么用?Python LineProfiler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了LineProfiler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: profile_lines

    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
开发者ID:SEC-i,项目名称:ecoControl,代码行数:42,代码来源:line_profile.py

示例2: ProfilingPanel

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})
开发者ID:ariesfath,项目名称:django-debug-toolbar-line-profiler,代码行数:60,代码来源:panel.py

示例3: profile_each_line

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
开发者ID:nextml,项目名称:NEXT,代码行数:9,代码来源:utils.py

示例4: profiled_func

 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()
开发者ID:Gild,项目名称:lifelines,代码行数:10,代码来源:utils.py

示例5: wrapper

 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
开发者ID:iamyogesh,项目名称:foodem,代码行数:15,代码来源:models.py

示例6: profiled_func

 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()
开发者ID:bytearchive,项目名称:dimagi-utils,代码行数:12,代码来源:profile.py

示例7: __init__

    def __init__(self, *args):
        self.profile = LineProfiler()

        if len(args) > 0:
            for func in args:
                if callable(func):
                    self.add_function(func)
开发者ID:hugadams,项目名称:trtools,代码行数:7,代码来源:profiler.py

示例8: profile

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()
开发者ID:gofia,项目名称:universal-portfolios,代码行数:18,代码来源:tools.py

示例9: process_view

 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
开发者ID:ariesfath,项目名称:django-debug-toolbar-line-profiler,代码行数:9,代码来源:panel.py

示例10: profile_list_deserialization

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()
开发者ID:pombredanne,项目名称:drf-benchmarks,代码行数:8,代码来源:test_serializer_to_internal_value_benchmarks.py

示例11: profiled_func

 def profiled_func(*args, **kwargs):
     try:
         lp = LineProfiler()
         lp.add_function(f)
         lp.enable_by_count()
         return f(*args, **kwargs)
     finally:
         lp.print_stats()
开发者ID:jiaxu825,项目名称:SUAVE,代码行数:8,代码来源:profile_tools.py

示例12: profile_list_serialization

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()
开发者ID:pombredanne,项目名称:drf-benchmarks,代码行数:8,代码来源:test_serializer_to_representation_benchmarks.py

示例13: DataReader_bin_test2

 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)
开发者ID:kirinsannnnnnnnnn,项目名称:Charpy,代码行数:8,代码来源:data_reader.py

示例14: main

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)
开发者ID:enthought,项目名称:pikos,代码行数:47,代码来源:function_events.py

示例15: benchmark

 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
开发者ID:miyaizu,项目名称:my_lib,代码行数:7,代码来源:myutil.py


注:本文中的line_profiler.LineProfiler类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。