Is there any simple way to benchmark python script?

前端 未结 10 774
借酒劲吻你
借酒劲吻你 2020-11-28 00:53

Usually I use shell command time. My purpose is to test if data is small, medium, large or very large set, how much time and memory usage will be.

Any t

10条回答
  •  青春惊慌失措
    2020-11-28 01:17

    snakeviz interactive viewer for cProfile

    https://github.com/jiffyclub/snakeviz/

    cProfile was mentioned at https://stackoverflow.com/a/1593034/895245 and snakeviz was mentioned in a comment, but I wanted to highlight it further.

    It is very hard to debug program performance just by looking at cprofile / pstats output, because they can only total times per function out of the box.

    However, what we really need in general is to see a nested view containing the stack traces of each call to actually find the main bottlenecks easily.

    And this is exactly what snakeviz provides via its default "icicle" view.

    First you have to dump the cProfile data to a binary file, and then you can snakeviz on that

    pip install -u snakeviz
    python -m cProfile -o results.prof myscript.py
    snakeviz results.prof
    

    This prints an URL to stdout which you can open on your browser, which contains the desired output that looks like this:

    and you can then:

    • hover each box to see the full path to the file that contains the function
    • click on a box to make that box show up on the top as a way to zoom in

    More profile oriented question: How can you profile a Python script?

提交回复
热议问题