Is there any simple way to benchmark python script?

前端 未结 10 758
借酒劲吻你
借酒劲吻你 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条回答
  •  萌比男神i
    2020-11-28 01:30

    If you don't want to write boilerplate code for timeit and get easy to analyze results, take a look at benchmarkit. Also it saves history of previous runs, so it is easy to compare the same function over the course of development.

    # pip install benchmarkit
    
    from benchmarkit import benchmark, benchmark_run
    
    N = 10000
    seq_list = list(range(N))
    seq_set = set(range(N))
    
    SAVE_PATH = '/tmp/benchmark_time.jsonl'
    
    @benchmark(num_iters=100, save_params=True)
    def search_in_list(num_items=N):
        return num_items - 1 in seq_list
    
    @benchmark(num_iters=100, save_params=True)
    def search_in_set(num_items=N):
        return num_items - 1 in seq_set
    
    benchmark_results = benchmark_run(
       [search_in_list, search_in_set],
       SAVE_PATH,
       comment='initial benchmark search',
    )  
    

    Prints to terminal and returns list of dictionaries with data for the last run. Command line entrypoints also available.

    If you change N=1000000 and rerun

提交回复
热议问题