“d.clear()”之间的差异和“d={}”

发布于 2024-08-31 00:47:48 字数 277 浏览 2 评论 0原文

在我的机器上,d.clear()d={} 之间的执行速度超过 100ns,所以我很好奇为什么一个会使用一个而不是另一个。

import timeit

def timing():
    d = dict()

if __name__=='__main__':
    t = timeit.Timer('timing()', 'from __main__ import timing')
    print t.repeat()

On my machine, the execution speed between d.clear() and d={} is over 100ns so am curious why one would use one over the other.

import timeit

def timing():
    d = dict()

if __name__=='__main__':
    t = timeit.Timer('timing()', 'from __main__ import timing')
    print t.repeat()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吖咩 2024-09-07 00:47:48

不同之处在于 d = {} 创建一个新字典,而 d.clear() 只是清空您已有的字典。如果代码中的其他位置保存了对字典的引用,那么这种细微的差异就很重要。在第一种情况下,其他对象不会看到任何更改,因为您没有修改原始字典。以下代码显示了这种操作上的差异。

创建新词典:

>>> d = {'foo': 'bar'}
>>> d2 = d
>>> d = {}
>>> d2
{'foo': 'bar'}

清除现有词典:

>>> d = {'foo': 'bar'}
>>> d2 = d
>>> d.clear()
>>> d2
{}

The difference is that d = {} creates a new dictionary and d.clear() just empties the dictionary you already have. This subtle difference matters if you have other places in your code holding references to your dictionary. In the first case those other objects won't see any change because you haven't modified the original dictionary. The following code shows this difference in action.

Creating a new dictionary:

>>> d = {'foo': 'bar'}
>>> d2 = d
>>> d = {}
>>> d2
{'foo': 'bar'}

Clearing the existing dictionary:

>>> d = {'foo': 'bar'}
>>> d2 = d
>>> d.clear()
>>> d2
{}
因为看清所以看轻 2024-09-07 00:47:48

d={} 创建一个新字典。

d.clear() 清除字典。

如果您使用 d={},则任何指向 d 的内容都将指向旧的 d。这可能会引入一个错误。

如果您使用d.clear(),那么任何指向d的东西现在都将指向已清除的字典,如果这不是您想要的,这也可能会引入一个错误。

另外,我不认为 d.clear() 会(在 CPython 中)释放 d 占用的内存。为了性能,当您删除元素时,CPython不会从字典中夺走内存,因为字典的通常用途是构建一个大字典,并且可能删除一些元素元素。在大多数用例中,重新分配内存(并确保哈希表保持一致)会花费太长时间。相反,它会用 turds(这是邮件列表上的技术术语)填充字典,这表明某个元素曾经在那里,但后来被删除了。我不完全确定 d.clear() 是否会这样做,但一一删除所有键肯定会这样做。

d={} creates a new dictionary.

d.clear() clears the dictionary.

If you use d={}, then anything pointing to d will be pointing to the old d. This may introduce a bug.

If you use d.clear(), then anything pointing at d will now point at the cleared dictionary, this may also introduce a bug, if that was not what you intended.

Also, I don't think d.clear() will (in CPython) free up memory taken up by d. For performance, CPython doesn't take the memory away from dictionaries when you delete elements, as the usual use for dictionaries is building a big dictionary, and maybe pruning out a few elements. Reassigning memory (and making sure the hash table stays consistent) would take too long in most use cases. Instead, it fills the dictionary with turds (that's the technical term on the mailing list), which indicate that an element used to be there but since got deleted. I'm not entirely sure if d.clear() does this though, but deleting all the keys one by one certainly does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文