切片最快的集合是什么?

发布于 2024-10-18 17:03:41 字数 412 浏览 4 评论 0原文

我需要执行以下操作:

collection.slice(x, y)
....
collection.slice(x+1, y+1)
...
collection.slice(x+n, y+n)

or

collection.slice(x-n, y-n)

通常需要执行这部分代码,因此我希望使其尽可能快。我应该选择什么集合? (可以是不可变的)。

PS 性能特征页面“应用”特征负责用于切片?

PPS 寻找专门针对该主题的文章、手册。

I need to execute the following operation:

collection.slice(x, y)
....
collection.slice(x+1, y+1)
...
collection.slice(x+n, y+n)

or

collection.slice(x-n, y-n)

Very often execution of this part of code is expected, so I want to make it as fast as possible. What collection should I choose? (can be immutable).

P.S. on Performance Characteristics page "apply" characteristic is responsible for slicing?

P.P.S looking for articles, manuals dedicated on subject.

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

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

发布评论

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

评论(2

々眼睛长脚气 2024-10-25 17:03:41

答案在某种程度上取决于 x 和 y 的距离,但通常最好使用普通数组。如果您不需要对切片执行任何操作,则使用视图可能会更快,但使用视图访问速度会较慢,因此最好远离它们。

无论如何,您肯定想要一些具有快速“应用”(对数或恒定时间)的东西,并且介于 ArrayVectorArrayBuffer>Array 是最快的,ArrayBuffer 大约慢 50%,而 Vector 在切片和使用切片后的每个元素方面又慢了大约两倍。

另外,请考虑滑动是否能达到您想要的效果。虽然不如直接切片那么快,但是非常方便。

The answer depends somewhat on how far apart x and y are, but you are generally best off using plain arrays. If you don't need to do anything with the slices, using views might be faster, but access is slower with views, so it's probably better to stay away from them.

Anyway, you certainly want something with fast "apply" (log or constant time), and between Array, Vector, and ArrayBuffer, Array is the fastest, ArrayBuffer is about 50% slower, and Vector is about twice as slow again for slicing and using every element that you've sliced.

Also, consider whether sliding will do what you want. It's not as fast as the direct slicing, but it's awfully convenient.

很酷又爱笑 2024-10-25 17:03:41

嗯,Vector 具有 logN 性能,但根据您的要求,常数因子可能太重。

另一方面,也许 range 能满足您的需求?如果确实如此,那么 SortedMap 很可能会以较小的常数因子提供 logN 性能。或许。

Well, Vector has logN performance, though the constant factor may be too heavy depending on your requirements.

On the other hand, maybe range does what you want? If it does, then a SortedMap might well provide a logN performance with smaller constant factor. Maybe.

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