Python profiling

Simple example

  1. Get data

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    # example from __main__.py
    import cProfile
    from pstats import Stats, SortKey

    from my_module.script import add_func


    def run_script():
    # call your function with some dummy data
    print(add_func(2,1))


    if __name__ == '__main__':
    do_profiling = True
    if do_profiling:
    with cProfile.Profile() as pr:
    run_script()

    with open('profiling_stats.txt', 'w') as stream:
    stats = Stats(pr, stream=stream)
    stats.strip_dirs()
    stats.sort_stats('time')
    stats.dump_stats('.prof_stats')
    stats.print_stats()
    else:
    start_game()
  2. Render data

Extensions on VSCodium