I have a question: when using Koltin to solve prob...
# announcements
i
I have a question: when using Koltin to solve problems in LeetCode, but the runtime is way higher than the Java runtime for the same solution, any idea why?
šŸ‘šŸ¼ 1
a
We'd need to see both solutions to tell whats going on
āž• 1
m
Have you tried a bogus solution for both to see if it’s overhead related to choosing Kotlin? I would hope there isn’t any, as it should only be counting runtime. i.e. a ā€˜solution’ that does println(ā€œI’m doneā€). Otherwise, what Andreas said
i
So for example the solution for reverse integer : https://leetcode.com/problems/reverse-integer
the Java Solution:
Copy code
class Solution {
    public int reverse(int x) {
        int rev = 0;
        while (x != 0) {
            int pop = x % 10;
            x /= 10;
            if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0;
            if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;
            rev = rev * 10 + pop;
        }
        return rev;
    }
}
The runtime is : 19ms
The same solution in Koltin :
Copy code
class Solution {
    fun reverse(x: Int): Int {
        var rev = 0
        var t = x
        while (t != 0) {
            var pop = t%10
            t /= 10
            if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0
            if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0
            rev = rev * 10 + pop
        }
        return rev
    }
}
The runtime is 176ms