如何在Python中提高time.strptime的效率?
How to improve the efficiency of time.strptime in Python?
我使用 Spyder 的分析器 运行 一个 python 脚本,它处理 700000 行数据,
而time.strptime
函数耗时超过60s(内置函数sort
仅耗时11s)
我该如何提高它的效率?有没有有效的时间操作模块?
核心代码片段在这里:
data = []
fr = open('big_data_out.txt')
for line in fr.readlines():
curLine = line.strip().split(',')
curLine[2] = time.strptime( curLine[2], '%Y-%m-%d-%H:%M:%S')
curLine[5] = time.strptime( curLine[5], '%Y-%m-%d-%H:%M:%S')
# print curLine
data.append(curLine)
data.sort(key = lambda l:( l[2], l[5], l[7]) )
#print data
result = []
for itm in data:
if itm[2] >= start_time and itm[5] <= end_time and itm[1] == cameraID1 and itm[4] == cameraID2:
result.append(itm)
来自此处给出的答案:
A faster strptime?
>>> timeit.timeit("time.strptime(\"2015-02-04 04:05:12\", \"%Y-%m-%d %H:%M:%S\")", setup="import time")
17.206257617290248
>>> timeit.timeit("datetime.datetime(*map(int, \"2015-02-04 04:05:12\".replace(\":\", \"-\").replace(\" \", \"-\").split(\"-\")))", setup="import datetime")
4.687687893159023
我使用 Spyder 的分析器 运行 一个 python 脚本,它处理 700000 行数据,
而time.strptime
函数耗时超过60s(内置函数sort
仅耗时11s)
我该如何提高它的效率?有没有有效的时间操作模块?
核心代码片段在这里:
data = []
fr = open('big_data_out.txt')
for line in fr.readlines():
curLine = line.strip().split(',')
curLine[2] = time.strptime( curLine[2], '%Y-%m-%d-%H:%M:%S')
curLine[5] = time.strptime( curLine[5], '%Y-%m-%d-%H:%M:%S')
# print curLine
data.append(curLine)
data.sort(key = lambda l:( l[2], l[5], l[7]) )
#print data
result = []
for itm in data:
if itm[2] >= start_time and itm[5] <= end_time and itm[1] == cameraID1 and itm[4] == cameraID2:
result.append(itm)
来自此处给出的答案: A faster strptime?
>>> timeit.timeit("time.strptime(\"2015-02-04 04:05:12\", \"%Y-%m-%d %H:%M:%S\")", setup="import time")
17.206257617290248
>>> timeit.timeit("datetime.datetime(*map(int, \"2015-02-04 04:05:12\".replace(\":\", \"-\").replace(\" \", \"-\").split(\"-\")))", setup="import datetime")
4.687687893159023