Python 3.x 优化读取txt文件所需的时间

Python 3.x 优化读取txt文件所需的时间,python-3.x,numpy,Python 3.x,Numpy,我想减少读取txt文件所需的时间。该文件包含如下所示的x和y坐标: {52.52, 13.38} {53.55, 10.} {48.14, 11.58} {50.95, 6.97} ... 目前,读取和计算12000个坐标的实际位置大约需要0.06秒,但我希望在这一半时间内完成 def read_coordinate_file(filename): points = [] file = open(filename, "r") for line in file: a, b = line.

我想减少读取txt文件所需的时间。该文件包含如下所示的x和y坐标:

{52.52, 13.38}
{53.55, 10.}
{48.14, 11.58}
{50.95, 6.97}
...
目前,读取和计算12000个坐标的实际位置大约需要0.06秒,但我希望在这一半时间内完成

def read_coordinate_file(filename):
points = []
file = open(filename, "r")
for line in file:
    a, b = line.strip("{}\n").split(",")
    points.append([get_x(float(b)), get_y(float(a))])
file.close()
return np.array(points)


def get_x(b, R=1):
return R*(pi*b)/180


def get_y(a, R=1):
temp = 90*pi+pi*a
return R*np.log(np.tan(temp/360))

如果我理解正确,这可以通过numpy数组实现。我试过使用np.loadtxt,但这比我当前的代码慢。有什么方法可以缩短计算时间吗?

肯定会同意这样的评论,即在Numpy中进行所有计算应该更快:

import numpy as np
from math import pi

def read_coordinate_file(filename):
    with open(filename, "r") as f:
        points = [tuple(map(float, line.strip("{}\n").split(','))) for line in f if line]
    arr = np.array(points, dtype=[('x','<f4'), ('y','<f4')])
    arr['x'] = arr['x'] * pi / 180
    arr['y'] = np.log(np.tan((90*pi + pi*arr['y'])/ 360))
    return arr
print(read_coordinate_file('data.txt'))
将numpy导入为np
从数学导入pi
def读取坐标文件(文件名):
打开(文件名为“r”)作为f:
points=[f-if-line中的行的元组(map(float,line.strip({}\n”).split(','))]

arr=np.array(points,dtype=[('x','s)这已经相当快了。在转换为Numpy数组后进行计算可能会得到改进。由于文本文件包含
{}
,因此常见的
csv
读取器将出现问题。一种变体是只收集
(float(b),float(A))
元组,然后在
上执行
获取x
获取y
处理(N,2)
float array.
np.log
np.tan
使用数组,但在使用标量时比
math
等效值慢。您可能会从中得到更好的建议,不要派人去查看代码-除非您在那里很活跃,并且准备自己回答这个问题。既然我返回了一个np.array,它应该去吗使用np.loadtxt,只使用np.array,而不使用列表?为了可读性,我会尝试将理解移到生成器中。