我应该将所有的一维数组替换为集合吗?

发布于 2025-01-23 20:25:20 字数 1448 浏览 0 评论 0原文

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

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

发布评论

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

评论(2

烈酒灼喉 2025-01-30 20:25:20

我认为dict或集合不比数组快(您是指listarray.Array.Array?)。原因是,当访问基本上会立即知道该项目在存储器中的位置时,而使用DICE,您必须计算位置(可能尝试几个不同的内存位置)。

换句话说,阵列比DICS或集合要简单得多,因此更快。

当您的基准分为另有说明时,也许您不是在对项目访问进行基准测试。也许您的基准测试太快了?

PS:确切地说,在某些情况下,DICT/设置比数组快,但我认为情况不是您的情况。当您有一个巨大的稀疏数组并且随机访问它时,可能会遇到计算机的高速缓存层次结构,将在最快的高速缓存层中具有很小的dict/set拟合。

I don't think dicts or sets are faster than arrays (you mean list or array.array?). The reason is that when accessing array items you basically know immediately where the item is going to be in memory, whereas with dict you have to compute (and possibly try few different memory locations) the location.

In other words, arrays are much more simple than dicts or sets and are therefore faster.

When your benchmark says otherwise, perhaps you are not benchmarking item access. Perhaps your benchmark ran too shortly?

PS: To be precise, there are cases where dict/set is faster than array but I assume that situation is not your case. When you have a huge sparse array and you access it randomly it can happen that the cache hierarchy of you computer imposes greater cost that having tiny dict/set fitting in the fastest cache layer.

等往事风中吹 2025-01-30 20:25:20

正如@wwii所说,“这取决于”。如果任何收藏量总是比另一个收藏品最好的,那么较慢的收藏可能就不存在。即使是listdictset的基本集合,每个收集都有不同的优势和弊端,并且有益于不同的目的。您可以在此处检查每个操作的复杂性: https://wiki.python.org nore.python.org/moin/moin/timecomplexity
如果我们比较,例如,列表和dict get and成员资格检查:

列表始终是o(1),所以在最坏情况下,它比dict o(n)更好。另一方面,dicts在c 操作中具有x的平均复杂性,而对于列表,它将为o(n)。

因此,您必须分析如何使用每个集合,其持有的数据等。才能真正判断哪种数据结构最适合您的需求。

话虽这么说,有一句著名的报价:早产优化是所有邪恶的根源 - 您真的需要 性能提高?由于每种类型的不同怪癖,是否值得打破某些东西?

As @wwii said, "it depends". If any of the collections were always best than the other, the slower ones probably wouldn't exist. Even the basic collections like list, dict and set each have got different upsides and downsides and are good for different purposes. You can check complexity of each operation here: https://wiki.python.org/moin/TimeComplexity
If we compare, for example list and dict get and membership check:

Lists get is always O(1), so it's better than dict O(n) in worst-case scenario. On the other hand, dicts have O(1) average complexity for x in c operation, while for lists it's gonna be O(n).

So you have to analyse how you use each of your collections, what data it holds etc. to really be able to tell what data structure fits your needs best.

That being said, there is a famous quote: Premature optimization is the root of all evil - do you really need that performance increase? Is it worth potentially breaking something due to different quirks of each type?

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