Skip to content

Commit 2f19513

Browse files
committed
RegionTimer: use atomic long instead of instance vars
1 parent 29ad652 commit 2f19513

File tree

1 file changed

+6
-18
lines changed

1 file changed

+6
-18
lines changed

src/main/scala/util/PerformanceTimer.scala

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
11
package util
22
import scala.collection.mutable
33
import scala.collection
4+
import java.util.concurrent.atomic.AtomicLong
45

56
case class RegionTimer(name: String) {
6-
private var total: Long = 0
7-
private var entered: Long = 0
8-
private var inside: Boolean = false
7+
private val total: AtomicLong = AtomicLong(0)
98

109
def within[T](body: => T): T = {
11-
enter()
10+
val begin = System.currentTimeMillis()
1211
val result = body
13-
exit()
12+
val finish = System.currentTimeMillis()
13+
val _ = total.addAndGet(finish - begin)
1414
result
1515
}
1616

17-
def enter() = {
18-
require(!inside)
19-
inside = true
20-
entered = System.currentTimeMillis()
21-
}
22-
23-
def exit() = {
24-
require(inside)
25-
inside = false
26-
total += (System.currentTimeMillis() - entered)
27-
}
28-
29-
def getTotal() = total
17+
def getTotal(): Long = total.get
3018

3119
override def toString() = {
3220
s"$name : ${getTotal()} (ms)"

0 commit comments

Comments
 (0)