soc*_*soc 6 arrays performance caching scala lazy-evaluation
考虑一下这个BigInt类,它应该缓存一些常见的值smallValues
:
object BigInt {
lazy val smallValues = Array(Zero, One, Two)
lazy val Zero = new BigInt(0, Array[Long]())
lazy val One = new BigInt(1, Array[Long](1))
lazy val Two = new BigInt(1, Array[Long](2))
private lazy val cacheSize = smallValues.length
def apply(num: Long): BigInt = {
// Is the number cached?
if (0 <= num && num < cacheSize) smallValues(num.toInt)
// Figure out the sign and make the number positive after that
else {
val (sign, value) = if (num < 0) (-1, num * -1) else (1, num)
new BigInt(sign, Array(value))
}
}
}
class BigInt private(val sign: Int, val num: Array[Long]) extends Ordered[BigInt] {
println("Constructing BigInt")
...
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是访问数组的一个元素会强制评估所有元素:
scala> BigInt.smallValues(0)
Constructing BigInt
Constructing BigInt
Constructing BigInt
res0: BigInt = BigInt@2c176570
Run Code Online (Sandbox Code Playgroud)
我怎么能解决这个问题?
编辑:看看提出的解决方案,我真的很想知道在没有进一步复杂化的情况下分配它们是否效率更高.你怎么看?
编辑我的答案,因为我认为这是您想做的事情的一个玩具示例,并且您的真实对象的构建成本非常昂贵,以至于懒惰给您带来了一些东西。如果问题显示的内容更像真实代码,那么懒惰就没有意义。惰性对象比严格对象更大且创建成本更高。尽管如此,我还是保留了以下代码,因为它确实展示了如何创建一个惰性包装器,并且它确实“工作”(从功能上正确的意义上说),即使它在成为一个好的意义上不“工作”您的用例的想法。
class Lazy[T] (expr : => T) {lazy val ! = expr}
object Lazy{def apply[T](expr : => T) = new Lazy({expr})}
class BigInt (val sign: Int, val num: Array[Long]) {
println("Constructing BigInt")
}
object BigInt {
val smallValues = Array(
Lazy(new BigInt(0, Array[Long]())),
Lazy(new BigInt(1, Array[Long](1))),
Lazy(new BigInt(1, Array[Long](2)))
)
private val cacheSize = smallValues.length.toLong
def apply(num: Long): BigInt = {
// Is the number cached?
if (0 <= num && num < cacheSize) smallValues(num.toInt)!
// Figure out the sign and make the number positive after that
else {
val (sign, value) = if (num < 0) (-1, num * -1) else (1, num)
new BigInt(sign, Array(value))
}
}
}
scala> BigInt(1)
Constructing BigInt
res0: BigInt = BigInt@c0dd841
scala> BigInt(1)
res1: BigInt = BigInt@c0dd841
scala> BigInt(2)
Constructing BigInt
res2: BigInt = BigInt@4a6a00ca
scala> BigInt(2)
res3: BigInt = BigInt@4a6a00ca
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
603 次 |
最近记录: |