python性能测试 cProfile,pstats

cProfile

import cProfile

def testcProfile(num):
    sum = 0;
    for item in range(num):
        sum += item;
    return sum;

if __name__ == '__main__':
    cProfile.run("testcProfile(123456)");
    """
        打印结果
        ncalls  函数调用次数
        tottime 函数总共运行时间
        percall 函数运行一次的平均时间
        cumtime 函数总计运行时间
        percall 函数运行一次的平均时间
        filename:lineno(function) 函数所在的文件名称,代码行数以及函数名称等信息
    """

将运行结果输出到本地文件目录中

import cProfile

def testcProfile(num):
    sum = 0;
    for item in range(num):
        sum += item;
    return sum;

if __name__ == '__main__':
    cProfile.run("testcProfile(123456)","d:\\testcProfile.text");
    """
        打印结果
        ncalls  函数调用次数
        tottime 函数总共运行时间
        percall 函数运行一次的平均时间
        cumtime 函数总计运行时间
        percall 函数运行一次的平均时间
        filename:lineno(function) 函数所在的文件名称,代码行数以及函数名称等信息
    """

pstats 读取对应测试报告

import pstats
def main():
    # 加载测试报告
    stats = pstats.Stats("D:\\testcProfile.text");
    # 测试时间排序
    stats.sort_stats("time");
    stats.print_stats();
if __name__ == '__main__':
    main();

猜你喜欢

转载自blog.csdn.net/weixin_44887276/article/details/114673073