我有许多日志文件,格式如下:
2012-09-12 23:12:00 other logs here
我需要提取时间字符串并比较两个日志记录之间的时间差。 我这样做了:
for line in log:
l = line.strip().split()
timelist = [int(n) for n in re.split("[- :]", l[0]+' ' + l[1])]
#now the timelist looks like [2012,9,12,23,12,0]
然后我有两条记录
d1 = datetime.datetime(timelist1[0], timelist1[1], timelist1[2], timelist1[3], timelist1[4], timelist1[5])
d2 = datetime.datetime(timelist2[0], timelist2[1], timelist2[2], timelist2[3], timelist2[4], timelist2[5])
delta = (d2-d1).seconds
问题是它运行缓慢,无论如何都要提高性能?提前感谢。
答案 0 :(得分:2)
你可以摆脱正则表达式并使用map
:
date_time = datetime.datetime
for line in log:
date, time = line.strip().split(' ', 2)[:2]
timelist = map(int, date.split('-') + time.split(':'))
d = date_time(*timelist)
.split(' ', 2)
会比.split()
更快,因为它只会分裂两次,而且只会在空格上,而不是在任何空格上。map(int, l)
比我上次检查时的[int(x) for x in l]
快。.strip()
。答案 1 :(得分:1)
你可以完全使用正则表达式,这可能会更快。
find_time = re.compile("^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})")
for line in log:
timelist = find_time.match(line)
if timelist:
d = datetime.datetime(*map(int, timelist.groups()))
答案 2 :(得分:1)
您也可以使用split
(date, time, log) = line.split(" ", 2)
timerecord = datetime.datetime.strptime(date+" "+time, "%Y-%m-%d %H:%M:%S")
然后在连续timedeltas
之间计算timerecord