0

这是有效的,但速度很慢。

我编写了一个自定义 .py 来将 .gpx 转换为 .kml。它像我需要的那样工作,但速度太慢了:对于 477k 的小 .gpx,它正在编写一个 207k 的 .kml 文件,需要 198 秒才能完成!那太荒谬了,我什至还没有达到肉的.gpx大小。

我的直觉是,它是stringIO.stringIO(x)如此缓慢。任何想法如何加快它?

感谢期待。

以下是关键片段:

f = open(fileName, "r")
x = f.read()
x = re.sub(r'\n', '', x, re.S) #remove any newline returns
name = re.search('<name>(.*)</name>', x, re.S)
print "Attachment name (as recorded from GPS device): " + name.group(1)

x = re.sub(r'<(.*)<trkseg>', '', x, re.S)  #strip header
x = x.replace("</trkseg></trk></gpx>",""); #strip footer
x = x.replace("<trkpt","\n<trkpt"); #make the file in lines
x = re.sub(r'<speed>(.*?)</speed>', '', x, re.S) #strip speed
x = re.sub(r'<extensions>(.*?)</extensions>', '', x, re.S) # strip out extensions

然后

#.kml header goes here
kmlTrack = """<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.ope......etc etc

然后

buf = StringIO.StringIO(x)
for line in buf:
            if line is not None:
                    timm = re.search('time>(.*?)</time', line, re.S)
                    if timm is not None:
                            kmlTrack += ("          <when>"+ timm.group(1)+"</when>\n")
                            checkSumA =+ 1
buf = StringIO.StringIO(x)
for line in buf:
            if line is not None:
                    lat = re.search('lat="(.*?)" lo', line, re.S)
                    lon = re.search('lon="(.*?)"><ele>', line, re.S)
                    ele = re.search('<ele>(.*?)</ele>', line, re.S)
                    if lat is not None:
                            kmlTrack += ("          <gx:coord>"+ lon.group(1) + " " + lat.group(1) + " " + ele.group(1) + "</gx:coord>\n")
                            checkSumB =+ 1
if checkSumA == checkSumB:
            #put a footer on
            kmlTrack += """     </gx:Track></Placemark></Document></kml>"""
else:
            print ("checksum error")
            return None

with open("outFile.kml", "a") as myfile:
            myfile.write(kmlTrack)
return ("succsesful .kml file-write completed in :" + str(c.seconds) + " seconds.")

再一次,这是可行的,但速度很慢。如果有人可以看到如何加快速度,请告诉我!干杯


更新

谢谢大家的建议。我是 Python 新手,很高兴听到有关分析的信息。发现了它。将其添加到我的脚本中。看起来它归结为一件事,209 秒的总运行时间中有 208 秒发生在一行中。这是一个片段:

 ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 ....

 4052    0.013    0.000    0.021    0.000 StringIO.py:139(readline)
 8104    0.004    0.000    0.004    0.000 StringIO.py:38(_complain_ifclosed)
    2    0.000    0.000    0.000    0.000 StringIO.py:54(__init__)
    2    0.000    0.000    0.000    0.000 StringIO.py:65(__iter__)
 4052    0.010    0.000    0.033    0.000 StringIO.py:68(next)
 8101    0.018    0.000    0.078    0.000 re.py:139(search)
    4    0.000    0.000  208.656   52.164 re.py:144(sub)
 8105    0.016    0.000    0.025    0.000 re.py:226(_compile)
   35    0.000    0.000    0.000    0.000 rpc.py:149(debug)
    5    0.000    0.000    0.010    0.002 rpc.py:208(remotecall)
 ......

每个呼叫有 4 次呼叫,每次 52 秒。cProfile 说它发生在第 144 行,但我的脚本只到 94 行。我该如何继续?非常感谢。

4

1 回答 1

3

OK thanks to all. the cProfile showed it was a re.sub call, though i initially wasn't sure which one - though with some trial and error, it didnt take long to isolate it. The solution was to fix the re.sub from being a 'greedy' to a 'non-greedy' call.

So the old header strip call was x = re.sub(r'<(.*)<trkseg>', '', x, re.S) #strip header now becomes x = re.sub(r'<?xml(.*?)<trkseg>', '', x, re.S) #strip header REALLY fast.

It now finshes even heavy .gxp conversions in zero seconds. What a difference a ? makes !

于 2013-05-05T05:53:48.143 回答