美文网首页
profile and pstats

profile and pstats

作者: VanCare | 来源:发表于2017-05-04 23:48 被阅读0次

    profile 对函数运行时间排序

    import profile
    profile.run('f()','result')   #result 为保存的结果,不能直接查看,使用pystats查看
    

    或命令行:

    python -m profile -s time example.py -o result
    

    pstats 对保存的结果查看

    import pstats
    p=pstats.Stats('result')
    p.sort_stats('time').print_stats(5)   #运行时间前5行
    p.sort_stats('cumulative').print_stats(0.1)   #总共时间前10%。
    

    和单元测试unittest合并

     if __name__ == '__main__':
        import profile
        profile.run('unittest.main()','result')
        import pstats
        p=pstats.Stats('result')
        p.sort_stats('time').print_stats(10)
        unittest.main()
    

    相关文章

      网友评论

          本文标题:profile and pstats

          本文链接:https://www.haomeiwen.com/subject/pnfjtxtx.html