@@ -1428,7 +1428,7 @@ Line # Hits Time Per Hit % Time Line Contents
1428
1428
```
1429
1429
1430
1430
### Call Graph
1431
- #### Generates a PNG image of call graph with highlighted bottlenecks:
1431
+ #### Generates a PNG image of call graph with highlighted bottlenecks.
1432
1432
``` python
1433
1433
# $ pip3 install pycallgraph
1434
1434
from pycallgraph import output, PyCallGraph
@@ -1440,54 +1440,6 @@ with PyCallGraph(output=graph):
1440
1440
< code_to_be_profiled>
1441
1441
```
1442
1442
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
-
1491
1443
1492
1444
Basic Script Template
1493
1445
---------------------
0 commit comments