Comparaison de l'efficacité de deux méthodes de lecture de txt en python

1. La méthode utilisée auparavant, le test prend environ 2,1 s

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. La méthode nouvellement modifiée, le test prend environ 8 ms

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

La première méthode prend trop de temps, et la deuxième amélioration est très évidente 

 

Je suppose que tu aimes

Origine blog.csdn.net/gbz3300255/article/details/108665652
conseillé
Classement