blob: e167ff04e8153031dd32ec3fdc3a019ac8812e21 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package scala.collection.immutable
package benchmarks
object RangeTest {
// not inlined any more, needs investigation
//
// class XXS {
// private val array = Array.range(0, 100)
// def tst = { var sum = 0; for (i <- 0 until array.length) sum += array(i); sum }
// }
var x: Int = 0
def foreachSum(max: Int): Int = {
var sum = 0
1 to max foreach (sum += _)
sum
}
def whileSum(max: Int) = {
var sum = 0
var num = 1
while (num <= max) {
sum += num
num += 1
}
sum
}
def show(max: Int, foreachNanos: Long, whileNanos: Long) {
val winner = if (foreachNanos < whileNanos) "foreachSum" else "whileSum"
val ratio = if (foreachNanos < whileNanos) foreachNanos.toDouble / whileNanos else whileNanos.toDouble / foreachNanos
println("1 to %d:, %12s wins, %.3f: foreach %.3f while %.3f".format(
max, winner, ratio,
foreachNanos.toDouble / 1000000L,
whileNanos.toDouble / 1000000L)
)
}
def run(max: Int) = {
val foreachFirst = util.Random.nextBoolean
val t1 = System.nanoTime
x = if (foreachFirst) foreachSum(max) else whileSum(max)
val t2 = System.nanoTime
x = if (foreachFirst) whileSum(max) else foreachSum(max)
val t3 = System.nanoTime
val foreachNanos = if (foreachFirst) t2 - t1 else t3 - t2
val whileNanos = if (foreachFirst) t3 - t2 else t2 - t1
show(max, foreachNanos, whileNanos)
}
def main(args: Array[String]): Unit = {
var max = if (args.isEmpty) 100 else args(0).toInt
while (max > 0) {
run(max)
run(max)
run(max)
max += (max / 7)
}
}
}
|