我在scala中嘗試了一些東西,發現下面的代碼演示了奇怪的性能問題。這裏內聯一個函數調用使計算需要4倍的時間(4秒vs 16秒)。Scala怪異的性能問題
package mypackage
object MyObject extends Application {
class Complex(val real: Double, val imaginary: Double) {
def +(operand: Complex): Complex = {
new Complex(real + operand.real, imaginary + operand.imaginary)
}
def *(operand: Complex): Complex = {
new Complex(real * operand.real - imaginary * operand.imaginary,
real * operand.imaginary + imaginary * operand.real)
}
def mag2 = real * real + imaginary * imaginary
}
val iSize = 1250
val jSize = 1250
val xMin = -2.5
val xMax = +1.5
val yMin = -2.0
val yMax = +2.0
val dx = (xMax - xMin)/iSize
val dy = (yMax - yMin)/jSize
val steps = 50
def iterate(c: Complex) = {
var z = c
var n = 0
do {
z = z * z + c
n += 1
} while (n < steps && z.mag2 < 4.0)
}
// Mandelbrot set using iteration in a function.
var startTime = System.currentTimeMillis()
for (i <- 0 until iSize; j <- 0 until jSize) {
val c = new Complex(xMin + i * dx, yMin + j * dy)
iterate(c)
}
System.out.println("Elapsed time: " + (System.currentTimeMillis() - startTime)/1000.0 + " (s)")
// Mandelbrot set using iteration.
startTime = System.currentTimeMillis()
for (i <- 0 until iSize; j <- 0 until jSize) {
val c = new Complex(xMin + i * dx, yMin + j * dy)
var z = c
var n = 0
do {
z = z * z + c
n += 1
} while (n < steps && z.mag2 < 4.0)
}
System.out.println("Elapsed time: " + (System.currentTimeMillis() - startTime)/1000.0 + " (s)")
}
任何想法爲什麼會發生這種情況? Interstingly在「迭代」函數之前添加「@inline」使其花費12 s vs 4 s。
你的scala和java版本是什麼? –