F# Set.union 参数顺序性能

发布于 2024-12-08 15:04:23 字数 165 浏览 1 评论 0原文

如果我知道两个集合中的一个比另一个大,是否有推荐的方法来调用 Set.union?

Set.union large small

Set.union small large

谢谢

Is there any recommended way to call a Set.union if I know one of the two sets will be larger than the other?

Set.union large small

or

Set.union small large

Thanks

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

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

发布评论

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

评论(3

沦落红尘 2024-12-15 15:04:24

在内部,集合表示为平衡树(您可以 在线查看来源)。在计算集合的并集时,该算法根据较大集合(树)的根的值将较小集合(树)拆分为一组更小的元素和一组更大的元素。分割总是在较小的集合上执行,以减少工作量。然后它递归地合并左右两个子集并执行一些重新平衡。

总结是,该算法并不真正取决于哪个集合是第一个参数以及哪个集合是第二个参数。它总是会根据集合的大小(作为数据结构的一部分存储)选择更好的选项。

Internally, sets are represented as ballanced tree (you can check the source online). When calculating the union of sets, the algorithm splits the smaller set (tree) based on the value from the root of the larger set (tree) into a set of smaller and a set of larger elements. The splitting is always performed on the smaller set to do less work. Then it recursively unions the two left and right subsets and performs some re-ballancing.

The summary is, the algorithm does not really depend on which of the sets is the first and which of them is the second argument. It will always choose the better option depending on the size of set (which is stored as part of the data structure).

烟花肆意 2024-12-15 15:04:24

您的问题背后的意图似乎是通过利用此函数实现的未记录功能来提高使用 Set.union 时的性能。但是,Set.union 将您从实现复杂性中抽象出来,仅留下与参数属性不可知union操作的集合理论含义。故意突破这个抽象层会对代码的复杂性和可维护性产生不利影响,应该避免。

尽管有时你别无选择,只能处理泄漏抽象Set.union绝对不是这样的。很高兴听到 Tomas Set.union 实现不存在泄漏抽象缺陷。

The intent behind your question seems to improve performance when using Set.union by exploiting undocumented features of this function implementation. But Set.union abstracts you from implementation complexity leaving just set-theoretic meaning of union operation that is agnostic to the argument properties. Purposedly breaking through this abstraction layer adversely affects complexity and maintainability of your code and should be avoided.

Although sometimes you have no choice but deal with leaky abstractions, Set.union is definitely not the case. And it is good to hear from Tomas that Set.union implementation does not have leaky abstraction flaws.

自找没趣 2024-12-15 15:04:24

做任何你想做的事。您还可以使用小+大大-小来获得差异(当然也可以小-大)。

Do whatever you want. You can also do small + large, and large - small for difference (of course also small - large).

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