1

I'm attempting to compare different run-times for simple blocks of code, but continue to get 0 returned. What can I do to get a better approximation for the execution time?

private var trackConstantTime: Long? = null
this.trackConstantTime = measureTimeMillis {
 /* determine if a given number is even or odd */
 var n = (0..(Int.MAX_VALUE)).random()
 if(n % 2 == 0) "Even" else "Odd"
}
println("O(1), Constant Time for fxConstantTime(...):${TimeUnit.MILLISECONDS.toSeconds(trackConstantTime!!)}")

Similarly if I'll attach another example that's returning 0 for the runtime.

private var trackLinearTime: Long? = null
private var uL: MutableList<Int> = mutableListOf()
for(i in 0..100){
 this.uL.add( ((0)..(100)).random() )
}
this.trackLinearTime = measureTimeMillis { 
 /* determine the maximum value in an unsorted array */
 var max: Int = 0
 for(i in 0 until uL.size) {
  if (uL[i] > max) max = uL[i]
  println(max)
 }
}
println("O(n), Linear Time for fxLinearTime(...):${TimeUnit.MILLISECONDS.toSeconds(trackLinearTime!!)}")
EvOlaNdLuPiZ
  • 600
  • 1
  • 4
  • 21
  • 1
    Be aware that there are many gotchas when trying to write microbenchmarks.  See [these](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) [questions](https://stackoverflow.com/questions/35930456/how-to-benchmark-a-kotlin-program). – gidds Jan 10 '20 at 00:02
  • @gidds any libraries you'd recommend for Kotlin? this is a mini project for fun btw. – EvOlaNdLuPiZ Jan 10 '20 at 00:59
  • Check out the questions I linked.  (I haven't done it myself.  But whenever this is discussed, the standard advice seems to be: don't roll your own, as it's surprisingly hard to get right.) – gidds Jan 10 '20 at 09:03

1 Answers1

1

Maybe try to measure time in nanoseconds:

this.trackLinearTime = measureNanoTime {
    ...
}
mac229
  • 4,319
  • 5
  • 18
  • 24
  • very nice. this worked. thank you. would you happen to also know why the measureTimeMillis didn't work? either way i'll mark as the accepted answer. thanks – EvOlaNdLuPiZ Jan 09 '20 at 20:28
  • 1
    1ms = 1000ns, so probably time in millis was too small – mac229 Jan 09 '20 at 20:28
  • interesting enough i should also mention that if i chance the `TimeUnit.MILLISECONDS.toSeconds(...)` to anything else will not convert correctly, it'll return 0 and I do suspect because we're dealing with very small numbers. Using the combination measureNanoTime as the block function and the output as `TimeUnit.MILLISECONDS.toSeconds(...)` will yield a result. – EvOlaNdLuPiZ Jan 09 '20 at 20:31
  • 1
    But the result will be wrong, because you have nanoseconds not milliseconds. :) You are dealing with really small numbers – mac229 Jan 09 '20 at 20:34
  • yes, it will yield a result, not a correct one, but nonetheless, it will yield a result. I'll update the post when I get the right conversions. if you have any other suggestion let me know. thanks :) – EvOlaNdLuPiZ Jan 09 '20 at 20:36
  • okay I figured out also why it was returning 0, it was a bit of syntax, has to be like this`whatever string: ${trackConstantTime!!}")` without the outter stuff i.e. TimeUnit.Milli.toSeconds() , now it's doing the conversion correctly. – EvOlaNdLuPiZ Jan 09 '20 at 21:52