私の小さな Python コード スニペットの実行時間を測定しようとしています。
理想的には、ある種のセットアップを実行し (非常に時間がかかります)、次にテスト コードを数回実行し、これらの実行の最小時間を取得したいと考えています。
timeit()
適切に思えましたが、セットアップを再実行せずに最小時間を取得する方法がわかりません。質問を示す小さなコード スニペット:
import timeit
setup = 'a = 2.0' # expensive
stmt = 'b = a**2' # also takes significantly longer than timer resolution
# this executes setup and stmt 10 times and the minimum of these 10
# runs is returned:
timings1 = timeit.repeat(stmt = stmt, setup = setup, repeat = 10, number = 1)
# this executes setup once and stmt 10 times but the overall time of
# these 10 runs is returned (and I would like to have the minimum
# of the 10 runs):
timings2 = timeit.repeat(stmt = stmt, setup = setup, repeat = 1, number = 10)