Kotlin run time metrics are not usable

These runs all used the exact same code. You can see that the variance in memory utilization is extremely high, as is the variance in run time.
With the metrics as inconsistent as they are, they are not usable as a meaningful indicator of anything.

image

This solution was run for Two Sum II

class Solution {
    fun twoSum(numbers: IntArray, target: Int): IntArray {
        for ((index, num) in numbers.withIndex()) {
            val remain = target - num
            val complementIndex = binarySearch(numbers, IntRange(index + 1, numbers.size - 1), remain) ?: -1
            if (complementIndex != -1) {
                return intArrayOf(index + 1, complementIndex + 1)
            }
        }
        return intArrayOf()
    }
    
    fun binarySearch(nums: IntArray, sourceRange: IntRange, target: Int): Int? {
        var range = sourceRange
        var pivot = (range.endInclusive - range.start) / 2 + range.start
        while (range.endInclusive - range.start > 1 && nums[pivot] != target) {
            range = if (target > nums[pivot]) {
                IntRange(pivot, range.endInclusive)
            } else {
                IntRange(range.start, pivot)
            }
            pivot = (range.endInclusive - range.start) / 2 + range.start
        }
        
        return when {
            nums[range.endInclusive] == target -> range.endInclusive
            nums[range.start] == target -> range.start
            nums[pivot] == target -> pivot
            else -> null
        }
    }
}

I did another test using a different solution, and this is the result
image

    fun twoSum(num: IntArray, target: Int): IntArray {
        var low = 0
        var high = num.size - 1
        fun get(): Int {
            return num[low] + num[high]
        }
        
        if (num[low] == num[high]) {
            return intArrayOf(low, high)
        } else {
            while (true) {
                if (get() < target) {
                    low++
                } else if (target < get()) {
                    high--
                } else {
                    return intArrayOf(++low, ++high)
                }
            }
        }
        return intArrayOf()
    }
	```
	As you can see, one of the runs was 184ms, and another run was 425ms. The code was the same. The memory utilization varies by 2MB, which is more unexpected than the run time differing.
Comments (3)