Python datetime.strptime()吃了大量的CPU时间 [英] Python datetime.strptime() Eating lots of CPU Time
问题描述
我有一些日志解析代码需要将时间戳记转换为datetime对象。我使用datetime.strptime,但是这个函数根据cProfile的cumtime列使用了大量的cputime。时间戳格式为 01 / Nov / 2010:07:49:33
。
目前的功能是:
new_entry ['time'] = datetime.strptime(
parsed_line ['day'] +
parsed_line ['month'] +
parsed_line ['year'] +
parsed_line ['hour'] +
parsed_line ['min'] +
parsed_line ['second']
,%d%b%Y%H%M%S
)
任何人都知道我可以如何优化这个?
如果这些是固定的宽度格式,那么就无需解析该行 - 您可以使用切片和字典查找直接获取字段。
<$ 4月4日,
'May':5,'Jun':1,'Feb':2,'Mar' :6,'Jul':7,'Aug':8,
'Sep':9,'Oct':10,'Nov':11,'Dec':12}
year = int (行[7:11])$ b $ b month = month_abbreviations [lin e [3:6]]
day = int(line [0:2])
hour = int(line [12:14])
minute = int(line [15:17 ]
second = int(line [18:20])
new_entry ['time'] = datetime.datetime(年,月,日,小时,分,秒)
I have some log parsing code that needs to turn a timestamp into a datetime object. I am using datetime.strptime but this function is using a lot of cputime according to cProfile's cumtime column. The timestamps are in the format of 01/Nov/2010:07:49:33
.
The current function is:
new_entry['time'] = datetime.strptime(
parsed_line['day'] +
parsed_line['month'] +
parsed_line['year'] +
parsed_line['hour'] +
parsed_line['minute'] +
parsed_line['second']
, "%d%b%Y%H%M%S"
)
Anyone know how I might optimize this?
解决方案 If those are fixed width formats then there is no need to parse the line - you can use slicing and a dictionary lookup to get the fields directly.
month_abbreviations = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4,
'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8,
'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
year = int(line[7:11])
month = month_abbreviations[line[3:6]]
day = int(line[0:2])
hour = int(line[12:14])
minute = int(line[15:17])
second = int(line[18:20])
new_entry['time'] = datetime.datetime(year, month, day, hour, minute, second)
Testing in the manner shown by Glenn Maynard shows this to be about 3 times faster.
这篇关于Python datetime.strptime()吃了大量的CPU时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!