みーのぺーじ

みーが趣味でやっているPCやソフトウェアについて.Python, Javascript, Processing, Unityなど.

Pythonで現在時刻を取得する関数の比較

Pythonで実際に測定してみました.

import datetime

from benchmarker import Benchmarker


def validate():
    native_now = datetime.datetime.now()
    assert native_now.tzinfo is None
    #
    native_utcnow = datetime.datetime.utcnow()
    assert native_utcnow.tzinfo is None
    #
    aware_now = datetime.datetime.now(tz=datetime.timezone.utc)
    assert aware_now.tzinfo == datetime.timezone.utc
    #
    aware_now_replace = datetime.datetime.now().replace(tzinfo=datetime.timezone.utc)
    assert aware_now_replace.tzinfo == datetime.timezone.utc
    #
    aware_utcnow_replace = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc
    )
    assert aware_utcnow_replace.tzinfo == datetime.timezone.utc


def run():
    with Benchmarker(1000000) as bench:

        @bench(None)
        def _(bm):
            for _ in bm:
                pass

        @bench("native-now")
        def _(bm):
            for _ in bm:
                datetime.datetime.now()

        @bench("native-utcnow")
        def _(bm):
            for _ in bm:
                datetime.datetime.utcnow()

        @bench("aware-now")
        def _(bm):
            for _ in bm:
                datetime.datetime.now(tz=datetime.timezone.utc)

        @bench("aware-now-replace")
        def _(bm):
            for _ in bm:
                datetime.datetime.now().replace(tzinfo=datetime.timezone.utc)

        @bench("aware-utcnow-replace")
        def _(bm):
            for _ in bm:
                datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)


if __name__ == "__main__":
    validate()
    run()

実行したところ以下のような結果が得られました.

% python main.py
## benchmarker:         release 4.0.1 (for python)
## python version:      3.11.2
## python compiler:     Clang 13.0.0 (clang-1300.0.29.30)
## python platform:     macOS-13.2.1-arm64-arm-64bit
## python executable:  ***
## cpu model:           Apple M1 
## parameters:          loop=1000000, cycle=1, extra=0

##                                       real    (total    = user    + sys)
(Empty)                                0.0127    0.0100    0.0100    0.0000
native-now                             0.2467    0.2500    0.2500    0.0000
native-utcnow                          0.1090    0.1100    0.1100    0.0000
aware-now                              0.1945    0.2000    0.2000    0.0000
aware-now-replace                      0.7166    0.7300    0.7200    0.0100
aware-utcnow-replace                   0.5850    0.5800    0.5800    0.0000

## Ranking                               real
native-utcnow                          0.1090  (100.0) ********************
aware-now                              0.1945  ( 56.0) ***********
native-now                             0.2467  ( 44.2) *********
aware-utcnow-replace                   0.5850  ( 18.6) ****
aware-now-replace                      0.7166  ( 15.2) ***

nativeな現在時刻を取得するならば,datetime.datetime.utcnow() が最速のようです.awareな現在時刻を取得するならば,datetime.datetime.now(tz=datetime.timezone.utc) が最速のようです.replace()メソッドを使用すると速度はかなり低下するようです.