简体   繁体   English

为什么以下两种方法之间存在性能差异?

[英]Why is there a performance difference between the following two methods?

After I submitted my solution to the Maximum Subarray problem on leetcode, it said my runtime was 1ms (beating ~66% submissions), then I made a couple of changes and resubmitted.在我在 leetcode 上提交了关于最大子数组问题的解决方案后,它说我的运行时间是 1 毫秒(超过了约 66% 的提交),然后我做了一些更改并重新提交。 This time, the runtime was 0ms (beating ~100%).这一次,运行时间为 0 毫秒(超过 ~100%)。 I do not quite understand what contributed to the reduction in time.我不太明白是什么导致了时间的减少。 Here the are two versions.这里有两个版本。

Original:原来的:

public int maxSubArray(int[] nums) {
        if (nums == null) return 0;
        if (nums.length == 1) return nums[0];

        int current_max = nums[0];
        int cur = 1;
        int max = current_max;

        while (cur < nums.length) {
            current_max = Math.max(nums[cur] + current_max, nums[cur]);
            if (current_max > max) {
                max = current_max;
            }
            cur++;
        }
        return max;
    }

Updated:更新:

public int maxSubArray(int[] nums) {
        if (nums == null) return 0;
        int l = nums.length;
        if (l == 1) return nums[0];

        int current_max = nums[0];
        int max = current_max;

        for (int i = 1; i < l; i++) {
            current_max = Math.max(nums[i] + current_max, nums[i]);
            if (current_max > max) {
                max = current_max;
            }
        }

        return max;
    }

The only two changes I did, were我做的唯一两个改变是

  1. Changing the while loop to a for loop - Apparently there is no difference ref将 while 循环更改为 for 循环 - 显然没有区别ref
  2. Store the length in a separate variable - Since Array does not compute the length and array.length is only fetching from an internal variable, I do not see any difference here either.将长度存储在单独的变量中 - 由于 Array 不计算长度,并且array.length仅从内部变量中获取,因此我在这里也看不到任何区别。

[EDIT] [编辑]

I agree.我同意。 leetcode's performance is not 100% accurate. leetcode 的性能并不是 100% 准确的。 Funny thing, I submitted my updated code again the other day, and suddenly now it's 0ms.有趣的是,前几天我再次提交了更新的代码,现在突然变成了 0ms。

First.第一的。 Every time you run the same code, time consumption is not completely equals每次运行相同的代码,时间消耗并不完全相等

Second.第二。 LeetCode just give you an approximate consumption, MAYBE < 1ms will return 0ms LeetCode 只是给你一个大概的消耗, MAYBE < 1ms 会返回 0ms

If you want more accurate time to consider test it with jmh如果您想要更准确的时间考虑使用 jmh 进行测试

https://openjdk.java.net/projects/code-tools/jmh/ https://openjdk.java.net/projects/code-tools/jmh/

https://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/ https://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/

Hope this may help希望这可能会有所帮助


update更新

if you want compare difference between the two method just put them in one class and如果您想比较两种方法之间的差异,只需将它们放在一个 class 和

javac filename.java

then然后

javap -c classname

you will get bytecode of these two methods, these byte codes are what real executed in jvm你会得到这两种方法的字节码,这些字节码是真正在 jvm 中执行的

reference:参考:

https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokespecial] https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokespecial]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM