案例类:始终复制与复制前检查差异的表现

发布于 2025-01-13 02:53:55 字数 443 浏览 0 评论 0原文

假设我有以下类:

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 技术交流群。

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

发布评论

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

评论(2

零度℉ 2025-01-20 02:53:55

简短的回答是“这取决于”。

仅当返回 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 after contains. So if you always call withoutWord 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.

甜心小果奶 2025-01-20 02:53:55
    def withoutWord(w:String):Foo = {
       val selected = ListBuffer[String]()
       var isSame = true
       words.foreach(c => {
          if(c==w) { isSame = false }
          else selected += c
       })
       if(isSame) this else copy(selected.toList)
    }

这样会更有效率。然而,它只是我们保存的参考副本。

    def withoutWord(w:String):Foo = {
       val selected = ListBuffer[String]()
       var isSame = true
       words.foreach(c => {
          if(c==w) { isSame = false }
          else selected += c
       })
       if(isSame) this else copy(selected.toList)
    }

This would be more effecient. However, its just a reference copy that we save.

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