Is there any simple way to benchmark python script?

前端 未结 10 754
借酒劲吻你
借酒劲吻你 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:38

    Have a look at nose and at one of its plugins, this one in particular.

    Once installed, nose is a script in your path, and that you can call in a directory which contains some python scripts:

    $: nosetests
    

    This will look in all the python files in the current directory and will execute any function that it recognizes as a test: for example, it recognizes any function with the word test_ in its name as a test.

    So you can just create a python script called test_yourfunction.py and write something like this in it:

    $: cat > test_yourfunction.py
    
    def test_smallinput():
        yourfunction(smallinput)
    
    def test_mediuminput():
        yourfunction(mediuminput)
    
    def test_largeinput():
        yourfunction(largeinput)
    

    Then you have to run

    $: nosetest --with-profile --profile-stats-file yourstatsprofile.prof testyourfunction.py
    

    and to read the profile file, use this python line:

    python -c "import hotshot.stats ; stats = hotshot.stats.load('yourstatsprofile.prof') ; stats.sort_stats('time', 'calls') ; stats.print_stats(200)"
    

提交回复
热议问题