Is there any simple way to benchmark python script?

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

    Be carefull timeit is very slow, it take 12 second on my medium processor to just initialize (or maybe run the function). you can test this accepted answer

    def test():
        lst = []
        for i in range(100):
            lst.append(i)
    
    if __name__ == '__main__':
        import timeit
        print(timeit.timeit("test()", setup="from __main__ import test")) # 12 second
    

    for simple thing I will use time instead, on my PC it return the result 0.0

    import time
    
    def test():
        lst = []
        for i in range(100):
            lst.append(i)
    
    t1 = time.time()
    
    test()
    
    result = time.time() - t1
    print(result) # 0.000000xxxx
    

提交回复
热议问题