0
0

Pythonコードの実行時間を計測する

Last updated at Posted at 2024-06-30

1. なんでそんなことするの?

Kaggleでニューラルネットとか使う創薬コンペに取り組んでいるのですが、Kaggle Notebookのランタイムの制約と戦っています(12時間/回)。ギリギリ時間切れでoutputが出て来ず、時間を何時間も無駄にするのは避けたいところ。
各コード・セルの処理時間を計測したいと思ったので、やり方のメモをします。

2. やり方

まずライブラリをインポートします。

import time

次に、処理時間を計測したいコードをtime.time()で挟みます。
time.time()は1970年1月1日 00:00:00 UTC(協定世界時)からの経過時間を秒単位で返します。

# 実行時間の計測開始
start_time = time.time()

#*  処理  *#
#*  処理  *#
#*  処理  *#

# 実行時間の計測終了
end_time = time.time()

# 実行時間の表示
elapsed_time = end_time - start_time
print(f"経過時間: {elapsed_time:.2f} seconds")

end_time − start_timeで経過秒数が分かります。

おわりに

他にも関連するtipsあれば教えてください!

0
0
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
0