@@ -1428,7 +1428,7 @@ Line # Hits Time Per Hit % Time Line Contents
14281428```
14291429
14301430### Call Graph
1431- #### Generates a PNG image of call graph with highlighted bottlenecks:
1431+ #### Generates a PNG image of call graph with highlighted bottlenecks.
14321432``` python
14331433# $ pip3 install pycallgraph
14341434from pycallgraph import output, PyCallGraph
@@ -1440,54 +1440,6 @@ with PyCallGraph(output=graph):
14401440 < code_to_be_profiled>
14411441```
14421442
1443- ### Profiling Decorators
1444- #### Prints runtime of a decorated function:
1445- ``` python
1446- from timeit import default_timer
1447- from datetime import timedelta
1448-
1449- def time_me (func ):
1450- def out (* args , ** kwargs ):
1451- start = default_timer()
1452- result = func(* args, ** kwargs)
1453- delta = timedelta(seconds = (default_timer() - start))
1454- print (f ' Function { func.__name__ } finished in { delta} ' )
1455- return result
1456- return out
1457- ```
1458-
1459- #### Saves run call profile of a decorated function to file:
1460- ``` python
1461- from cProfile import Profile
1462- from pstats import Stats
1463-
1464- def profile_me (func ):
1465- def out (* args , ** kwargs ):
1466- profile = Profile()
1467- result = profile.runcall(func, * args, ** kwargs)
1468- filename = f ' profile_ { func.__name__ } .txt '
1469- with open (filename, ' w' ) as stream:
1470- stats = Stats(profile, stream = stream)
1471- stats.strip_dirs().sort_stats(' tottime' )
1472- stats.print_stats(20 )
1473- print (f " Profile saved as ' { filename} ' " )
1474- return result
1475- return out
1476- ```
1477-
1478- #### Prints arguments and output of a decorated function:
1479- ``` python
1480- def trace_me (func ):
1481- def out (* args , ** kwargs ):
1482- result = func(* args, ** kwargs)
1483- arg_list = [repr (x) for x in args]
1484- arg_list += [f ' { k} = { v!r } ' for k, v in kwargs.items()]
1485- arg_str = ' , ' .join(arg_list)
1486- print (f ' { func.__name__ } ( { arg_str} ) = { result!r } ' )
1487- return result
1488- return out
1489- ```
1490-
14911443
14921444Basic Script Template
14931445---------------------
0 commit comments