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!!)}")