teachpyx.tools.profiling¶
- class teachpyx.tools.profiling.ProfileNode(filename: str, line: int, func_name: str, nc1: int, nc2: int, tin: float, tall: float)[source][source]¶
Graph structure to represent a profiling.
- Paramètres:
filename – filename
line – line number
func_name – function name
nc1 – number of calls 1
nc2 – number of calls 2
tin – time spent in the function
tout – time spent in the function and in the sub functions
- add_called_by(pnode: ProfileNode)[source][source]¶
This function is called by these lines.
- add_calls_to(pnode: ProfileNode, time_elements)[source][source]¶
This function calls this node.
- as_dict(filter_node=None, sort_key=SortKey.LINE)[source][source]¶
Renders the results of a profiling interpreted with function @fn profile2graph. It can then be loaded with a dataframe.
- Paramètres:
filter_node – display only the nodes for which this function returns True, if None, the default function removes built-in function with small impact
sort_key – sort sub nodes by…
- Renvoie:
rows
- static filter_node_(node, info=None) bool [source][source]¶
Filters out node to be displayed by default.
- Paramètres:
node – node
info – if the node is called by a function, this dictionary can be used to overwrite the attributes held by the node
- Renvoie:
boolean (True to keep, False to forget)
- property key¶
Returns
file:line
.
- to_json(filter_node=None, sort_key=SortKey.LINE, as_str=True, **kwargs) str [source][source]¶
Renders the results of a profiling interpreted with function @fn profile2graph as JSON.
- Paramètres:
filter_node – display only the nodes for which this function returns True, if None, the default function removes built-in function with small impact
sort_key – sort sub nodes by…
as_str – converts the json into a string
kwargs – see
json.dumps()
- Renvoie:
rows
- to_text(filter_node=None, sort_key=SortKey.LINE, fct_width=60) str [source][source]¶
Prints the profiling to text.
- Paramètres:
filter_node – display only the nodes for which this function returns True, if None, the default function removes built-in function with small impact
sort_key – sort sub nodes by…
- Renvoie:
rows
- teachpyx.tools.profiling.profile(fct: Callable, sort: str = 'cumulative', rootrem: str | None = None, as_df: bool = False, return_results: bool = False, **kwargs) str [source][source]¶
Profiles the execution of a function.
- Paramètres:
fct – function to profile
sort – see
pstats.Stats.sort_stats()
rootrem – root to remove in filenames
as_df – return the results as a dataframe and not text
return_results – if True, return results as well (in the first position)
kwargs – additional parameters used to create the profiler, see cProfile.Profile
- Renvoie:
raw results, statistics text dump (or dataframe is as_df is True)
(
Source code
,png
,hires.png
,pdf
)
- teachpyx.tools.profiling.profile2df(ps: Stats, as_df: bool = True, clean_text: bool | None = None, verbose: bool = False, logf=None)[source][source]¶
Converts profiling statistics into a Dataframe.
- Paramètres:
ps – an instance of pstats
as_df – returns the results as a dataframe (True) or a list of dictionaries (False)
clean_text – function to clean function names
verbose – verbosity
logf – logging function
- Renvoie:
a DataFrame
import pstats from onnx_array_api.profiling import profile2df ps = pstats.Stats('bench_ortmodule_nn_gpu6.prof') df = profile2df(pd) print(df)
- teachpyx.tools.profiling.profile2graph(ps: Stats, clean_text: Callable | None = None, verbose: bool = False, logf: Callable | None = None) ProfileNode [source][source]¶
Converts profiling statistics into a graphs.
- Paramètres:
ps –
an instance of pstats
clean_text – function to clean function names
verbose – verbosity
logf – logging function
- Renvoie:
an instance of class @see cl ProfileNode
pyinstrument has a nice display to show time spent and call stack at the same time. This function tries to replicate that display based on the results produced by module
cProfile
. Here is an example.<<<
import time from teachpyx.tools.profiling import profile, profile2graph def fct0(t): time.sleep(t) def fct1(t): time.sleep(t) def fct2(): fct1(0.1) fct1(0.01) def fct3(): fct0(0.2) fct1(0.5) def fct4(): fct2() fct3() ps = profile(fct4)[0] root, nodes = profile2graph(ps, clean_text=lambda x: x.split("/")[-1]) text = root.to_text() print(text)
>>>
fct1 -- 3 3 -- 0.00004 0.61168 -- :11:fct1 (fct1) <built-in method time.sleep> -- 3 3 -- 0.61164 0.61164 -- ~:0:<built-in method time.sleep> (<built-in method time.sleep>) +++ fct4 -- 1 1 -- 0.00001 0.81252 -- :25:fct4 (fct4) fct2 -- 1 1 -- 0.00001 0.11104 -- :15:fct2 (fct2) fct1 -- 2 2 -- 0.00002 0.11103 -- :11:fct1 (fct1) +++ fct3 -- 1 1 -- 0.00002 0.70146 -- :20:fct3 (fct3) fct0 -- 1 1 -- 0.00002 0.20079 -- :7:fct0 (fct0) <built-in method time.sleep> -- 1 1 -- 0.20077 0.20077 -- ~:0:<built-in method time.sleep> (<built-in method time.sleep>) +++ fct1 -- 1 1 -- 0.00002 0.50066 -- :11:fct1 (fct1) +++ <built-in method time.sleep> -- 4 4 -- 0.81241 0.81241 -- ~:0:<built-in method time.sleep> (<built-in method time.sleep>)