cprofile#
- class cprofile(sort='cumtime', columns='default', mintime=0.001, maxitems=100, maxfunclen=40, maxpathlen=40, stripdirs=True, show=True)[source]#
Bases:
prettyobj
Function profiler, built off Python’s built-in cProfile
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.The profiler can be used either with the
enable()
anddisable()
commands, or as a context block. See examples below for details.Default columns of output are:
‘func’: the function being called
‘cumpct’: the cumulative percentage of time taken by this function (including subfunctions)
‘selfpct’: the percentage of time taken just by this function (excluding subfunctions)
‘cumtime’: the cumulative time taken by this function
‘selftime’: the time taken just by this function
‘calls’: the number of calls
‘path’: the file and line number
- Parameters:
sort (str) – the column to sort by (default “cumpct”)
columns (str) – what columns to show; options are “default” (above), “brief” (just func, cumtime, selftime), and “full” (as default plus percall and separate line numbers)
mintime (float) – exclude function times below this value
maxitems (int) – only include up to this many functions in the output
maxfunclen (int) – maximum length of the function name to print
maxpathlen (int) – maximum length of the function path to print
stripdirs (bool) – whether to strip folder information from the file paths
show (bool) – whether to show results of the profiling as soon as it’s complete
Examples:
import sciris as sc import numpy as np class Slow: def math(self): n = 1_000_000 self.a = np.arange(n) self.b = sum(self.a) def plain(self): n = 100_000 self.int_list = [] self.int_dict = {} for i in range(n): self.int_list.append(i) for j in range(10): self.int_dict[i+j] = i+j def run(self): self.math() self.plain() # Option 1: as a context block with sc.cprofile() as cpr: slow = Slow() slow.run() # Option 2: with start and stop cpr = sc.cprofile() cpr.start() slow = Slow() slow.run() cpr.stop()
New in version 3.1.6.Methods
- to_df(sort=None, mintime=None, maxitems=None, maxfunclen=None, maxpathlen=None, columns=None)[source]#
Parse data into a dataframe
See class docstring for arguments