profile#
- profile(run, follow=None, private='__init__', include=None, exclude=None, print_stats=True, verbose=True, *args, **kwargs)[source]#
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
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