Weekly - issue 43

How to Benchmark a Python Statement

  • time.perf_counter()

    from time import perf_counter
    
    # record start time
    time_start = perf_counter()
    
    # execute the statement
    data = [i*i for i in range(100000000)]
    
    # record end time
    time_end = perf_counter()
    
    # calculate the duration
    time_duration = time_end - time_start
    
    # report the duration
    print(f'Took {time_duration} seconds')
  • timeit

    from timeit import timeit
    
    # benchmark the statement
    time_duration = timeit('[i*i for i in range(100000000)]', number=1)
    
    # report the duration
    print(f'Took {time_duration} seconds')

    OR

    # The command executes the statement one time and
    # reports the best time (smallest time) over 3 repetitions of executing the statement
    python -m timeit -n 1 -r 3 "[i*i for i in range(100000000)]"

How to Share Large Data Between Processes in Python

  • Inherit a copy of the structure in each process

    It is the fastest way.

  • Pass a copy of the structure to each task via an argument

    It is the slowest way.

  • Initialize process workers with a copy of the structure once