profile#

class profile(run, follow=None, private='__init__', include=None, exclude=None, unwrap=True, skipzero=False, do_run=True, verbose=True, *args, **kwargs)[source]#

Bases: prettyobj

Profile the line-by-line time required by a function.

Interface to the line_profiler library.

Note: sc.profile() shows the time taken by each line of code, in the order the code appears in. sc.cprofile() shows the time taken by each function, regardless of where in the code it appears.

Parameters:
  • run (function) – The function to be run

  • follow (function) – The function, list of functions, class, or module to be followed in the profiler; if None, defaults to the run function

  • private (bool/str/list) – if True and a class is supplied, follow private functions; if a string/list, follow only those private functions (default '__init__')

  • include (str) – if a class/module is supplied, include only functions matching this string

  • exclude (str) – if a class/module is supplied, exclude functions matching this string

  • unwrap (bool) – if true (default), then unwrap functions wrapped by decorators (otherwise, the decorator is profiled)

  • skipzero (bool) – skip functions with 0 time (i.e. that were not run); default false (i.e. do include them)

  • do_run (bool) – whether to run immediately (default: true)

  • print_stats (bool) – whether to print the statistics of the profile to stdout (default True)

  • verbose (bool) – list the functions to be profiled

  • args (list) – Passed to the function to be run

  • kwargs (dict) – Passed to the function to be run

Returns:

LineProfiler (by default, the profile output is also printed to stdout)

Example:

def slow_fn():
    n = 10000
    int_list = []
    int_dict = {}
    for i in range(n):
        int_list.append(i)
        int_dict[i] = i
    return

class Foo:
    def __init__(self, a=0):
        self.a = a

    def outer(self):
        for i in range(100):
            self.inner()

    def inner(self):
        for i in range(1000):
            self.a += 1

# Profile a function
sc.profile(slow_fn)

# Profile a class or class instance
foo = Foo()
sc.profile(run=foo.outer, follow=foo)

# Profile the constructor for Foo
f = lambda a: Foo(a)
sc.profile(run=f, follow=Foo.__init__, a=10) # "a" is passed to the function
New in version 3.2.0: allow class and module arguments for “follow”; “private” argument
New in version 3.2.2: converted to a class
New in version 3.2.4: “merge” method, “unwrap” argument

Methods

parse_follow(strict=False)[source]#

Do processing on the functions

run(disp=None)[source]#

Run profiling

Parameters:

disp (bool) – whether to display results (self.disp()) after run; if None, use self.verbose value

merge(other, inplace=False, swap=False, overwrite=True)[source]#

Allow multiple profilers to be combined (to be able to do combined stats)

Parameters:
  • other (sc.profile) – the other sc.profile instance to merge

  • inplace (bool) – if True, modify this instance in place

  • swap (bool) – if True, put “other” first

  • overwrite (bool) – if True, overwrite duplicates (with the one that took more time)

New in version 3.2.4.
sort(bytime=1, copy=False)[source]#

Sort or unsort by time.

Parameters:

bytime (int) – if 1, sort by increasing time (default); if -1, sort by decreasing; if 0, do not sort by time

disp(bytime=1, maxentries=10, skiprun=False)[source]#

Display the results of the profiling

plot(bytime=1, maxentries=10, figkwargs=None, barkwargs=None)[source]#

Plot the time spent on each function.

Parameters:
  • bytime (bool) – if True, order events by total time rather than actual order

  • maxentries (int) – how many entries to show

  • figkwargs (dict) – passed to plt.figure()

  • barkwargs (dict) – passed to plt.bar()