Last active
May 18, 2018 04:10
-
-
Save devlights/300d9a6aa17a5e592ab1e6a28444a7c8 to your computer and use it in GitHub Desktop.
[python] 最速日付解析ライブラリ ciso8601 のサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
日付解析ライブラリ:ciso8601のサンプル | |
最速らしいので試してみた。 | |
""" | |
import argparse | |
import time | |
from datetime import datetime, timedelta | |
import ciso8601 | |
def main(args): | |
epoch = datetime.utcfromtimestamp(0) | |
datetimes = [ | |
(epoch + timedelta(seconds=i)).isoformat() | |
for i in range(args.count) | |
] | |
f = _call_ciso8601 if args.mode == 'ciso8601' else _call_datetime | |
start = time.perf_counter() | |
f(datetimes, args) | |
elapsed = round(time.perf_counter() - start, 3) | |
print(f'[{args.mode}]\tcount: {args.count}\telapsed: {elapsed}secs') # noqa | |
def _call_ciso8601(datetimes, args): | |
f = ciso8601.parse_datetime_unaware if args.call_unaware else ciso8601.parse_datetime # noqa | |
print(f'[method] {f.__name__}') | |
for d in datetimes: | |
f(d) | |
def _call_datetime(datetimes, args): | |
for d in datetimes: | |
datetime.strptime(d, '%Y-%m-%dT%H:%M:%S') | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--count', type=int, default=100000) | |
parser.add_argument('--mode', default='ciso8601', choices=['ciso8601', 'datetime']) # noqa | |
parser.add_argument('--call-unaware', action='store_true', default=False) | |
args = parser.parse_args() | |
main(args) |
naiveな日付の場合は、parse_datetime_unaware
を使ったほうが速いとREADMEに記載があった。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
以下のような結果となった。確かに速い。
通常の datetime.strptime の場合
ciso8601