What are you guys using to time your solutions?
# advent-of-code
j
What are you guys using to time your solutions?
a
Can't speak for everyone but I'm just wrapping my solution in the following code:
Copy code
repeat(5) {
        val time = measureTimeMillis {
              ....
        }
        println("Elapsed Time ${time}ms")
    }
The repeats just ensure that you
warmup
the JVM by loading classes etc so that you can ignore the 1st run and get an average of subsequent runs.
g
I'm running a full jmh bench on circle ci for java, should be easy for kotlin as well, https://circleci.com/gh/gklijs/advent_of_code_2018/163
e
Inspired by @andyb, I rewrote my benchmarking algorithm. https://github.com/edgars-supe/advent-of-code/blob/master/src/main/kotlin/lv/esupe/aoc/Puzzle.kt A puzzle's file then has
fun main(args: Array<String>) = solve { Day7() }
and it prints out the time to initialize (if you preprocess some stuff to use for both parts), then averages 5 runs of each part and prints the result and time taken. (hmm, it also means that it include time taken to read the input file, meh, but it's noteworthy if the init time is unusually large)
j
@andyb Whoo I optimized my code by 100x by taking better measurements 😃
g
You have to be careful, for example if you do nothing with the result your method might not actually run anymore, jmh got a 'black hole' for that
j
Hmm. Added one last round of run and printed the result (to convince myself it did the computation) and the timing was consistent
🤷‍♂️