Python のドキュメントによると、さまざまなオペレーティング システムでの時間関数の精度に関係しています。
デフォルトのタイマー機能はプラットフォームに依存します。Windows では、time.clock() の粒度はマイクロ秒ですが、time.time() の粒度は 1/60 秒です。Unix では、time.clock() は 1/100 秒の粒度を持ち、time.time() ははるかに正確です。どちらのプラットフォームでも、デフォルトのタイマー関数は、CPU 時間ではなくウォール クロック時間を測定します。これは、同じコンピューターで実行されている他のプロセスがタイミングに干渉する可能性があることを意味します... Unix では、time.clock() を使用して CPU 時間を測定できます。
timeit.py
のコードから直接プルするには:
if sys.platform == "win32":
# On Windows, the best timer is time.clock()
default_timer = time.clock
else:
# On most other platforms the best timer is time.time()
default_timer = time.time
さらに、ランタイム コードの設定を直接処理します。使用するtime
場合は、自分で行う必要があります。もちろん、これはあなたの時間を節約します
Timeit のセットアップ:
def inner(_it, _timer):
#Your setup code
%(setup)s
_t0 = _timer()
for _i in _it:
#The code you want to time
%(stmt)s
_t1 = _timer()
return _t1 - _t0
パイソン 3:
Python 3.3 以降、time.perf_counter()
(システム全体のタイミング) またはtime.process_time()
(プロセス全体のタイミング) を使用できますtime.clock()
。
from time import process_time
t = process_time()
#do some stuff
elapsed_time = process_time() - t
新しい関数process_time
には、スリープ中に経過した時間が含まれません。
Python 3.7+:
Python 3.7 以降では、 process_time_ns()
which に似てprocess_time()
いますが時間をナノ秒で返すものも使用できます。