测试python程序运行时间

发布时间:2019-08-30 09:29:51编辑:auto阅读(1930)

    import time
    import decimal
    
    # text decimal_sum
    print("decimal:")
    start =time.time()
    
    sum = decimal.Decimal('0')
    for x in range(100):
        sum += decimal.Decimal(x)
    
    end = time.time()
    # print('Running time: %s Seconds'%(end-start))
    
    print('Running time: {} Seconds'.format(end-start))
    print("===============================")
    #text_int
    print("sum_text")
    start =time.time()
    sum = 0
    
    for x in range(100):
        sum += x
    
    end = time.time()
    print('Running time: {} Seconds'.format(end-start))
    print("===============================")
    #print("int")
    print("int")
    start =  time.time()
    
    print(4.0)
    end = time.time()
    print('Running time: {} Seconds'.format(end-start))
    print("===============================")
    
    print("str")
    start =  time.time()
    
    print("sdfgh")
    end = time.time()
    print('Running time: {} Seconds'.format(end-start))
    print("===============================")

    text code

    经由测试,print()花费时间大约是1e-6s,decimal运行花费的时间大约是int的三倍。

关键字