Is there any simple way to benchmark python script?

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

    The timeit module was slow and weird, so I wrote this:

    def timereps(reps, func):
        from time import time
        start = time()
        for i in range(0, reps):
            func()
        end = time()
        return (end - start) / reps
    

    Example:

    import os
    listdir_time = timereps(10000, lambda: os.listdir('/'))
    print "python can do %d os.listdir('/') per second" % (1 / listdir_time)
    

    For me, it says:

    python can do 40925 os.listdir('/') per second
    

    This is a primitive sort of benchmarking, but it's good enough.

提交回复
热议问题