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 3 years have passed since last update.

Python_timeモジュール

Last updated at Posted at 2021-11-28

timeモジュール

Pyhtonの時間に関する関数が使用できるbuild in moduleである。

unixtimeを取得する

unixtimeとは1970/01/01からの秒数のことである。

import time

print(time.time())

1638073472.6348443

実行時間を計測する

import time

before = time.time()
~~~計測したいプログラム~~~
after = time.time()
print(f"{after-before}")

0.00014710426330566406秒

.sleep(sec) 実行まで指定した秒数待機する

before = time.time()
time.sleep(10)
after = time.time()
print(f"{after-before:.2f}")

10.01秒

デコレーターにする

import time
from functools import wraps

def stop_watch(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        print(f"実行時間(sec):{time.time()-start:2f}")
        return result
    return wrapper


@stop_watch
def my_sleep(sec):
    time.sleep(sec)
    print(f"{sec}秒待ちました")


my_sleep(10)

10秒待ちました
実行時間(sec):10.017276

0
1
1

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?