0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ナノ秒まで測れる時間計測デコレータ

Last updated at Posted at 2022-11-27

ナノ秒まで測れる簡単な方法が見つからなかったので作成しました。
python3.7以降で使えます。

時間計測デコレータ
import time
from functools import wraps

def proc_time(f):
    @wraps(f)
    def _wrapper(*args, **kwargs):
        st = time.perf_counter_ns()
        res = f(*args, **kwargs)
        t = (time.perf_counter_ns() - st) * 0.000000001
        print(f'{format(t,".2E")} sec')
        return res
    return _wrapper
実行テスト
@proc_time
def a():
    pass

a()
結果
7.70E-07 sec

軽い関数を用意して100nsオーダーまで計測できることを確認しました。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?