Skip to content

Commit e8a420b

Browse files
committed
✨ GameLoop logs time since start of each round.
1 parent b597378 commit e8a420b

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/main/scala/com/truelaurel/codingame/challenge/GameLoop.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ class GameLoop[Context, State, Action](
1010
turns: Int = 200
1111
) {
1212
def run(): Unit = {
13-
val time = System.nanoTime()
13+
CGLogger.startOfRound = System.nanoTime
1414
val initContext = gameIO.readContext
15-
CGLogger.info("GameInit elt: " + (System.nanoTime() - time) / 1000000 + "ms")
15+
CGLogger.info("GameInit")
1616
(1 to turns).foldLeft(initContext) {
1717
case (c, turn) =>
1818
val state = gameIO.readState(turn, c)
19+
CGLogger.startOfRound = System.nanoTime
1920
CGLogger.info(state)
20-
val time = System.nanoTime()
2121
val actions = myPlayer.react(state)
22-
CGLogger.info("GameReact elt: " + (System.nanoTime() - time) / 1000000 + "ms")
22+
CGLogger.info("GameReact")
2323
gameIO.writeAction(state, actions)
2424
accumulator.accumulate(c, state, actions)
2525
}

src/main/scala/com/truelaurel/codingame/logging/CGLogger.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ object CGLogger {
77

88
val info = 0
99
val debug = 1
10+
val warn = -1
1011

1112
var current = info
1213

14+
var startOfRound: Long = 0
15+
16+
def time: Long = (System.nanoTime - startOfRound) / 1000000
17+
1318
def debug(message: Any): Unit = log(message, debug)
1419

1520
def info(message: Any): Unit = log(message, info)
1621

1722
private def log(message: Any, level: Int): Unit = {
1823
if (level <= current) {
19-
System.err.println(message)
24+
System.err.println(s"$time ms - $message")
2025
}
2126
}
2227

0 commit comments

Comments
 (0)