A有几个"TimeA:0.216/1,TimeB:495.761/1,TimeC:2.048/2,TimeD:0.296/1"
形式的日志行(语法是timerName:time / instances)`这就是我解析它的方式
ServiceTimer = namedtuple("ServiceTimer", ["timerName", "time", "instances"])
timers = []
for entry in line.split(","):
name, rest = entry.split(":")
time, instances = rest.split("/")
timers.append(ServiceTimer(name, float(time), int(instances)))
有没有更好的方法,它还需要快速,因为有数百万条日志行。任何指针都会很棒。
答案 0 :(得分:2)
我测试了三个版本:
结果有点让我感到惊讶。我的结果显示“string”.split非常快,比示例正则表达式处理更快。为了使正则表达式更快,您必须使用内存映射文件并忘记逐行处理。
这是temp.py中的来源:
def process1():
results = []
with open('temp.txt') as fptr:
for line in fptr:
for entry in line.split(','):
name, rest = entry.split(":")
time, instances = rest.split("/")
results.append((name, float(time), int(instances)))
return len(results)
def process2():
from re import finditer
results = []
with open('temp.txt') as fptr:
for line in fptr:
for match in finditer(r'([^,:]*):([^/]*)/([^,]*)', line):
results.append(
(match.group(1), float(match.group(2)), int(match.group(3))))
return len(results)
def process3():
from re import finditer
import mmap
results = []
with open('temp.txt', 'r+') as fptr:
fmap = mmap.mmap(fptr.fileno(), 0)
for match in finditer(r'([^,:]*):([^/]*)/([^,\r\n]*)', fmap):
results.append(
(match.group(1), float(match.group(2)), int(match.group(3))))
return len(results)
我在“temp.txt”文本文件中测试了这些函数,其中包含了一百万个示例行的副本。结果如下:
In [8]: %time temp.process1()
CPU times: user 10.24 s, sys: 0.00 s, total: 10.24 s
Wall time: 10.24 s
Out[8]: 4000000
In [9]: %time temp.process2()
CPU times: user 12.63 s, sys: 0.00 s, total: 12.63 s
Wall time: 12.63 s
Out[9]: 4000000
In [10]: %time temp.process3()
CPU times: user 9.43 s, sys: 0.00 s, total: 9.43 s
Wall time: 9.43 s
Out[10]: 4000000
因此,忽略逐行处理和内存映射文件的正则表达式版本比示例代码快7%。示例正则表达式代码比您的示例慢23%。
故事的道德:永远是基准。
答案 1 :(得分:1)
根据@zaftcoAgeiha的建议,使用正则表达式:
from re import finditer
line = "TimeA:0.216/1,TimeB:495.761/1,TimeC:2.048/2,TimeD:0.296/1"
[ m.groups( ) for m in finditer( r'([^,:]*):([^/]*)/([^,]*)', line ) ]
你会得到:
[('TimeA', '0.216', '1'),
('TimeB', '495.761', '1'),
('TimeC', '2.048', '2'),
('TimeD', '0.296', '1')]
对于类型转换,您可以使用group
方法:
[ ( m.group(1), float( m.group(2) ) , int( m.group(3) ))
for m in finditer( r'([^,:]*):([^/]*)/([^,]*)', line ) ]
编辑:解析首先编译模式所需的整个文件,然后使用列表理解而不是append
:
from re import compile
regex = compile( r'([^,:]*):([^/]*)/([^,]*)' )
with open( 'fname.txt', 'r' ) as fin:
results = [ ( m.group(1), float( m.group(2) ) , int( m.group(3) ))
for m in regex.finditer( line ) for line in fin]
答案 2 :(得分:0)
也许用更少的线..
for entry in line.split(','):
split_line = entry.split(":")[1].split('/')
timers.append(ServiceTimer(entry.split(':')[0],float(split_line[0]),int(split_line[1])