blob: c01e7cb46e8c2a13a231128ffa74799836642e37 (
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
|
import scala.collection.mutable.HashMap
object Test {
var dummy: Long = 0L
var _foreach: Long = 0L
var _iterator: Long = 0L
def numbers: Seq[Int] = 1 to 1000000
val map: HashMap[Int, Int] = HashMap(numbers zip numbers: _*)
@inline final def timed(body: => Unit): Long = {
val start = System.nanoTime
body
System.nanoTime - start
}
def go(xs: Iterable[Int], reps: Int) = {
_foreach = 0L
_iterator = 0L
0 until reps foreach { _ =>
_foreach += timed(xs foreach (dummy += _))
_iterator += timed(xs.iterator foreach (dummy += _))
}
" foreach avg " + (_foreach / reps) + "\n iterator avg " + (_iterator / reps) + "\n"
}
def go2(xs: collection.Map[Int, Int], reps: Int) = {
_foreach = 0L
_iterator = 0L
def incDummy(nums: (Int, Int)) = {
dummy += nums._1
dummy -= nums._2
}
0 until reps foreach { _ =>
_foreach += timed(xs foreach incDummy)
_iterator += timed(xs.iterator foreach incDummy)
}
" foreach avg " + (_foreach / reps) + "\n iterator avg " + (_iterator / reps) + "\n"
}
def main(args: Array[String]): Unit = {
println("map.keys:")
go(map.keys, 10) // warm
println(go(map.keys, 10))
println("map.values:")
go(map.values, 10) // warm
println(go(map.values, 10))
println("map:")
go2(map, 10) // warm
println(go2(map, 10))
println("// pay me no mind ... " + dummy)
}
}
|