简体   繁体   English

在案例类中def vs lazy val

[英]def vs lazy val in case class

I have a DAO object which I defined as a case class. 我有一个DAO对象,我将其定义为案例类。

case class StudentDAO(id: Int) {
  def getGPA: Double = // Expensive database lookup goes here
  def getRank: Int = // Another expensive database operation and computation goes here
  def getScoreCard: File = // Expensive file lookup goes here
}

I would naturally make getGPA and getRank and getScoreCard def s and not val s because I don't want them to be computed before they may be used. 我自然会使getGPAgetRankgetScoreCard def而不是val s因为我不想在它们被使用之前计算它们。

What would be the performance impact if I marked these methods as lazy val s instead of def s? 如果我将这些方法标记为lazy val s而不是def s,会对性能产生什么影响? The reason I want to make them lazy val s is: I do not want to recompute the rank each time for a Student with id "i". 我想让他们lazy val的原因是:我不想每次为id为“i”的学生重新计算等级。

I am hoping that this will not be marked as duplicate because there are several questions as below which are mostly about differences: 我希望这不会被标记为重复,因为有几个问题如下,主要是差异:

When to use val, def, and lazy val in Scala? 何时在Scala中使用val,def和lazy val?

def or val or lazy val for grammar rules? def或val或lazy val用于语法规则?

`def` vs `val` vs `lazy val` evaluation in Scala Scala中的`def` vs`val` vs`lazy val`评估

Scala Lazy Val Question 斯卡拉懒惰的问题

This question is mainly aimed towards the expenses (tradeoffs between CPU vs. memory) in making a method a lazy val for costly operations and what would one suggest over other and why? 这个问题主要针对的是费用(CPU与内存之间的权衡)使method成为昂贵的操作的lazy val ,以及为什么会提出其他建议?为什么?

EDIT: Thank you for the comment @om-nom-nom. 编辑:感谢您对@ om-nom-nom的评论。 I should have been more clear with what I was looking for. 我应该更清楚我正在寻找什么。

I read here: 我在这里读到:

Use of lazy val for caching string representation 使用lazy val缓存字符串表示

that string representation of the object is cached (see @Dave Griffith's answer ). 缓存对象的字符串表示(请参阅@Dave Griffith的回答 )。 More precisely I am looking at the impact of Garbage Collection if I made it a lazy val instead of def 更确切地说,如果我把它变成lazy val而不是def我正在研究垃圾收集的影响

Seems pretty straightforward to me: 对我来说似乎很简单:

I don't want them to be computed before they may be used. 我不希望它们在被使用之前被计算出来。 [...] I do not want to recompute the rank each time for a Student with id "i". [...]我不想每次为id为“i”的学生重新计算排名。

Then use lazy val and that's it. 然后使用lazy val ,就是这样。

def is used when the value may change for each call, typically because you pass parameters, val won't change but will be computed right away. 当每次调用的值都可能改变时使用def ,通常是因为你传递参数, val不会改变,但会立即计算。

A lazy val for an "ordinary" reference type (eg, File ) has the effect of creating a strong reference the first time it is evaluated. “普通”引用类型(例如, File )的lazy val具有在第一次评估时创建强引用的效果。 Thus, while it will avoid re-evaluations of an unchanging value, it has the obvious cost of keeping the computed value in memory. 因此,虽然它将避免重新评估不变的值,但它具有将计算值保持在存储器中的明显成本。

For primitive values (or even lightweight objects, like File ), this memory cost usually isn't much of an issue (unless you're holding lots of Student objects in memory). 对于原始值(甚至是轻量级对象,如File ),这种内存成本通常不是问题(除非你在内存中持有大量的Student对象)。 For a heavy reference, though (eg, a large data structure), you might be better off using a weak reference, some other caching approach, or just computing the value on-demand. 但是,对于重要的参考(例如,大型数据结构),您最好使用弱引用,其他一些缓存方法,或者只是按需计算值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM