F# Set.union 参数顺序性能
如果我知道两个集合中的一个比另一个大,是否有推荐的方法来调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在内部,集合表示为平衡树(您可以 在线查看来源)。在计算集合的并集时,该算法根据较大集合(树)的根的值将较小集合(树)拆分为一组更小的元素和一组更大的元素。分割总是在较小的集合上执行,以减少工作量。然后它递归地合并左右两个子集并执行一些重新平衡。
总结是,该算法并不真正取决于哪个集合是第一个参数以及哪个集合是第二个参数。它总是会根据集合的大小(作为数据结构的一部分存储)选择更好的选项。
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).
您的问题背后的意图似乎是通过利用此函数实现的未记录功能来提高使用 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. ButSet.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 thatSet.union
implementation does not have leaky abstraction flaws.做任何你想做的事。您还可以使用
小+大
和大-小
来获得差异(当然也可以小-大
)。Do whatever you want. You can also do
small + large
, andlarge - small
for difference (of course alsosmall - large
).