Python 在 for 循环中销毁实例时的性能提升

Python performance gains when destructing instances within a for loop

我正在使用 Python 脚本读取一些 ASCII 文件,操作它们的值并获得输出。计算是在 class 实例化中完成的,类似于伪形式

def __init__(input)
  self.input = input
  self.output = function of input 

伪代码,加上问号之间有争议的部分,是

open file
read lines
for each lines in file: 
    split line
    construct class instance with input from split-line values
    store instance.output in a help variable (list)
    ?? delete class instance ??
further processing of the help variable
etc

删除class实例是节省时间和内存的障碍还是机会?问题的规模很大(不到 100 万行)。

我很清楚我宁愿从二进制文件中读入,但目前这是行不通的。此外,我选择 class 构造是因为它很优雅,也许随着脚本的开发,我可以从封装中获得更多好处。但是,如果有人建议我可以在这个阶段放弃它。

为什么要写伪代码,而不仅仅是 python?无论如何,在 python 中删除 class 实例是没有意义的,如果你只是想在下一个循环中用新实例覆盖名称。当没有引用时,解释器将自动删除内存中的对象。

所以这两个选项花费的时间几乎相同(见下文):

from collections import UserList

def with_del():
    for i in range(10000):
        x = UserList([i])
        del x

def without_del():
    for i in range(10000):
        x = UserList([i])

%timeit with_del()
8.19 ms ± 188 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit without_del()
8.04 ms ± 92.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

with_del 可能需要稍长的时间,因为 运行 有一个额外的字节码指令。

Python 当垃圾收集过程中不再引用实例时,它会自动为您销毁一个实例,因此您不应该自己做,除非您确实希望在仍有引用时删除该实例到它。

在您的情况下,每次迭代都会创建新实例,并且由于您仅将实例派生的输出而不是实例本身存储到列表中,因此您不会保留对旧实例的任何引用下一次迭代中的实例,因此垃圾回收过程将以有效的方式为您销毁实例,因此您不必担心自己动手。自己做实际上会更慢,因为您将使用 Python 代码进行删除,而不是使用纯粹用 C 实现的垃圾收集。