1.之前用的方法, 测试耗时在2.1s左右
def ReadTxtData(BINARY,himg ,wimg,txtname):
a = np.loadtxt(txtname, skiprows=0, dtype=bytes).astype(str)
for u in range(himg):
for v in range(wimg):
gray = a[u][v]
if gray == '1':
BINARY[u, v] = 255
2.新修改的方法,测试耗时在8ms左右
def ReadTxtData1(BINARY,himg ,wimg,txtname):
with open(txtname, encoding="utf-8") as file:
file.seek(0, 0)
u = 0
for eachLine in file:
for v in range(wimg):
gray = eachLine[v]
if gray == '1':
BINARY[u, v] = 255
u += 1
第一种方法耗时太多了,第二种提升非常明显