案例类:始终复制与复制前检查差异的表现
假设我有以下类:
case class Foo(words: List[String])
我想创建该类的一个方法成员,用于从列表中删除一个元素。我想知道这两种实现之间是否存在显着差异:
def withoutWord(w: String): Foo = copy(words = words.filterNot(_ == w))
def withoutWord(w: String): Foo = {
if(words contains w) copy(words = words.filterNot(_ == w))
else this
}
是否最好检查修改并在没有任何更改的情况下返回当前实例,或者保存这次并始终复制?就我而言,单词不应超过 20 个元素,并且该类还有其他几个字段。
Lets say i have the following class:
case class Foo(words: List[String])
I want to make a method member of this class that removes an element from the list. I am wondering if there is a notable difference between these two implementations:
def withoutWord(w: String): Foo = copy(words = words.filterNot(_ == w))
def withoutWord(w: String): Foo = {
if(words contains w) copy(words = words.filterNot(_ == w))
else this
}
Is it better to check for modifications and return the current instance if nothing has changed or save this time and always copy? In my case, words should rarely exceeds 20 elements and the class has several other fields.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的回答是“这取决于”。
仅当返回 false 时,执行
contains
检查才是成功,当您在contains
之后最终进行过滤时,它比仅进行过滤要慢。因此,如果您总是使用已经存在的单词来调用withoutWord
,那么它肯定会更慢,如果您从不这样做,它肯定会更快,并且命中率没有总体差异。如果冗余对象和集合停留一段时间,这会给 GC 带来压力(如果它们是短暂的,大多数 GC 将以这样一种方式处理它,即额外的工作是无穷小的),并且可能会导致性能下降/未来的稳定性问题:在这种情况下,可能有必要每次都支付
contains
检查的可预测成本。The short answer is "it depends".
Doing the
contains
check is a win only when it returns false, it is slower than just filtering when you end up filtering aftercontains
. So if you always callwithoutWord
with a word that's is already there, it's definitely slower and if you never do, it's definitely faster and there's a hit rate at which there's no overall difference.If the redundant objects and collections are staying around for a while, that will be stressful on the GC (if they're short-lived, most GCs will handle it in such a way that the extra work is infinitesimal) and might lead to performance/stability issues down the road: in that situation, it may be warranted to pay the predictable cost of the
contains
check every time.这样会更有效率。然而,它只是我们保存的参考副本。
This would be more effecient. However, its just a reference copy that we save.