与Java相比,为什么Kotlin提交的速度明显慢? [英] Why Kotlin submission is significantly slow compared to Java?

查看:170
本文介绍了与Java相比,为什么Kotlin提交的速度明显慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对科特林很陌生.因此,为了练习,我尝试使用它来解决LeetCode中的问题.这是我今天解决的一个问题:
不同的子序列(Leetcode)
首先,我尝试解决Java中的问题:

I am very new to Kotlin. So to practice, I tried using it to solve problems in LeetCode. Here is one problem that I solved today:
Distinct Subsequence(Leetcode)
First I tried solving the problem in Java:

class Solution {
    public int numDistinct(String s, String t) {
        int[][] dp = new int[t.length() + 1][s.length() + 1];
        for (int i = 0; i <= s.length(); i++) {
            dp[0][i] = 1;
        }
        for (int i = 1; i <= t.length(); i++) {
            for (int j = 1; j <= s.length(); j++) {
                if (t.charAt(i - 1) != s.charAt(j - 1)) {
                    dp[i][j] = dp[i][j - 1];
                } else {
                    dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1];
                }
            }
        }
        return dp[t.length()][s.length()];
    }
}

这是该算法的运行时:

Runtime: 6 ms
Memory Usage: 39.4 MB

然后我尝试用Kotlin(下面的代码)解决问题:

And then I tried solving the problem in Kotlin(Code below):

class Solution {
    fun numDistinct(s: String, t: String): Int {
        var dp = Array(t.count() + 1) {Array(s.count() + 1) {0} }
        for (i in 0 until s.count()) {
            dp[0][i] = 1;
        }
        for (i in 1..t.count()) {
            for (j in 1..s.count()) {
                if (t[i - 1] != s[j - 1]) {
                    dp[i][j] = dp[i][j - 1];
                } else {
                    dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1];
                }
            }
        }
        return dp[t.count()][s.count()]
    }
}

令人震惊的是,上述Kotlin实现的算法的运行时如下:

Shockingly the runtime is below for the above Kotlin implemented algorithm:

Runtime: 176 ms
Memory Usage: 34.2 MB

问题是,与Java相比,为什么Kotlin解决方案要花费这么多时间?

The question is why Kotlin solution is taking so much time to run compared to Java?

推荐答案

我认为这可以归结为他们的后端如何启动虚拟机并计算时间.因为这大约也需要200毫秒,这很荒谬:

I think this can be chalked up to how their backend is spinning up the VM and measuring time. Because this also takes roughly 200ms, which is ridiculous:

class Solution {
    fun numDistinct(s: String, t: String): Int {
        return 3
    }
}

我怀疑您可以依靠大约200毫秒的预热时间,而不管代码有多简单.尽管奇怪的是,我尝试对这两者进行编辑以在返回前重复一百万次算法,并且Kotlin代码(在按如下所述进行等效性调整之后)始终显示出较低的运行时间.

I suspect you can count on about 200ms of warm-up time regardless of how simple the code is. Although, weirdly, I tried editing both of these to repeat the algorithm a million times before returning, and the Kotlin code (after adjusting for equivalence as described below) consistently shows a lower runtime.

尽管如此,您的代码并不完全等同于Java.另一个答案已经提到您的内部数组类型.

Your code isn't exactly equivalent to the Java, though. The other answer mentioned already your inner array type.

与性能无关,但是您的第一个循环在Java中比在Kotlin中填充了更多的空间.

And not performance related, but your first loop fills one more space in Java than in Kotlin.

这篇关于与Java相比,为什么Kotlin提交的速度明显慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆