https://github.com/closeio/ciso8601
ciso8601
converts ISO 8601 or RFC 3339 date time strings into Python datetime objects.
Since it's written as a C module, it is much faster than other Python libraries. Tested with Python 2.7, 3.4, 3.5, 3.6, 3.7.
当然,如果格式固定已知,那么从字符串里拆出指定位数的字符再转化为int也不算太慢
https://www.peterbe.com/plog/fastest-python-datetime-parser
def f1(datestr): return datetime.datetime.strptime(datestr, '%Y-%m-%dT%H:%M:%S.%fZ') def f2(datestr): return ciso8601.parse_datetime(datestr) def f3(datestr): return datetime.datetime( int(datestr[:4]), int(datestr[5:7]), int(datestr[8:10]), int(datestr[11:13]), int(datestr[14:16]), int(datestr[17:19]), )
f1
: baselinef2
: 13 times fasterf3
: 6 times faster