Java 在同一循环的迭代之间,局部变量是重用的还是重新分配的?

Java 在同一循环的迭代之间,局部变量是重用的还是重新分配的?,java,performance,Java,Performance,我一直明白,在循环中定义局部变量并不会降低它的速度,因为它们在同一循环的迭代之间被重用 我惊讶地发现,当我将局部变量的定义移到循环之外时,它会显著减少内存(39.4Mb vs 40 Mb) 在同一循环的迭代之间,局部变量是重用的还是重新分配的 我也看到了 重复零问题():给定整数的固定长度数组arr,重复每次出现的情况 为零,将其余元素向右移动 请注意,超出原始数组长度的元素不会被删除 书面的 对输入数组执行上述修改,不返回 任何与你的职能有关的事情 之后 内存使用率提高(39.4 Mb对40

我一直明白,在循环中定义局部变量并不会降低它的速度,因为它们在同一循环的迭代之间被重用

我惊讶地发现,当我将局部变量的定义移到循环之外时,它会显著减少内存(39.4Mb vs 40 Mb)

在同一循环的迭代之间,局部变量是重用的还是重新分配的

我也看到了

重复零问题():给定整数的固定长度数组arr,重复每次出现的情况 为零,将其余元素向右移动

请注意,超出原始数组长度的元素不会被删除 书面的

对输入数组执行上述修改,不返回 任何与你的职能有关的事情

之后

内存使用率提高(39.4 Mb对40 Mb)

静态无效数组复制(int[]arr,int begin,int end,int n){
int destination=0;//>>>>>>>>>>>>>>
对于(int i=end+1;i>=begin;i--){
目的地=i+n;
如果(目的地
关于您的问题 我一直明白,在循环中定义局部变量 不要减慢速度,因为它们在的迭代之间被重用 同样的循环

在循环中声明局部变量不会减慢它的速度吗

  • 是的,你说得对。声明局部变量不会增加时间复杂度,或者如果它确实稍微改变了运行时,那么它就太微不足道了

  • LeetCode的运行时和内存度量非常不准确,尤其是运行时。例如,我刚刚重新提交了以下解决方案,结果显示为39.6 MB,几天前,在没有字节更改的情况下,同样的解决方案显示为43.3 MB:

  • 他们的测试用例通常是有限的,因为我想这是昂贵的,因此他们的基准测试是没有价值的
公共最终类解决方案{
公共静态最终无效重复零(int[]arr){
int countZero=0;
对于(int index=0;index
  • 总的来说,最好主要关注渐进有效的算法,因为基准测试有很多“how-to”,我们希望在独立的测试系统中拥有真正好的资源(CPU、内存等)

工具书类
  • 有关更多详细信息,请参阅,您可以在其中找到大量解释良好的公认解决方案,包括低复杂度算法和渐近/分析

您是如何测量内存使用率的?你确定这是因为这个,而不是其他灭绝的因素吗?我在leetcode系统中运行了它。它告诉您内存使用情况和运行时性能,并与其他提交进行比较。即使内存报告不准确,只做此更改就可以将内存与其他解决方案的比较从50%(之前)提高到89%(之后)。所以它确实起了作用。你知道leetcode是如何测量的吗?除非我们确切知道他们测量的是什么,我们只能猜测。有很多很多因素会影响到这样的决策,而有一个可生产、可执行的方法来获取你引用的数字对于任何关于这个话题的实际讨论都是至关重要的。所以,要么在本地复制这些(或类似的)数字,要么找出leetcode是如何做到的。@JoachimSuer是我最初理解正确的-在循环中声明局部变量不会减慢它的速度?@likejudo:就这样的广义语句而言,该语句通常是正确的。谢谢你的回答。我是否需要在Eclipse中运行程序(使用插件来测量堆大小),以正确测量堆大小?关于这个问题,我最初的理解正确吗?在循环中声明局部变量不会降低它的速度?请你在回答SO问题时加上你的答复好吗?
import java.util.Arrays;

/**
 * algorithm: the zeroes divide the array into sub-arrays or subsets.
 * we move or shift the elements exactly once, to their final resting place R.I.P. ;)
 * The last subset will be shifted n0s places, the one before it, n0s -1 places and so on...
 * O(n)
 * @author likejudo
 *
 */
public class DuplicateZeroes {
    static void arrayCopy(int[] arr, int begin, int end, int n) {
        for (int i = end + 1; i >= begin ; i--) {
            int destination = i + n;
            if (destination < arr.length) {
                arr[destination] = arr[i];
            }
        }
    }

    public static void duplicateZeros(int[] arr) {
        int n0s = 0; // number of zeroes
        int last0At = -1; // last zero at index
        int boundary = 0; // rightmost boundary

        // find total n0s, last0At
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 0) {
                n0s++;
                last0At = i;
            }
        }
//      System.out.format("n0s=%d last0At=%d \n", n0s, last0At);

        // if no zeroes or all zeroes, we are done
        if(n0s == 0 || n0s == arr.length) {
            return;
        }
        
        boundary = arr.length - n0s;

        while (n0s > 0) {
        //  System.out.format("before arrayCopy(%s, %d, %d, %d) ", Arrays.toString(arr), last0At, boundary, n0s);
            // move subset of all elements from last0At till boundary-1, by n0s spaces.
            arrayCopy(arr, last0At, boundary, n0s);
            // set start of subset to 0
            arr[last0At] = 0;
//          System.out.format("after arrayCopy : %s assigned arr[last0At=%d]=0\n", Arrays.toString(arr),last0At);
            // update boundary
            boundary = last0At - 1;
            // next subset to the left will have one less zero
            n0s--;
            last0At--;

            // find the next last zer0 At index
            while (last0At > 0 && arr[last0At] != 0)
                last0At--;
            // if no more...
            if (last0At <0 || arr[last0At] != 0) {
                return;
            }
        }
    }

    public static void main(String[] args) {
        // input: [1, 0, 2, 3, 0, 4, 5, 0]
        // output: [1, 0, 0, 2, 3, 0, 0, 4]

        int[] arr = {0,0,0,0,0,0,0};
        System.out.println("input:  " + Arrays.toString(arr));

        duplicateZeros(arr);
        System.out.println("output: " + Arrays.toString(arr));

    }

}
    static void arrayCopy(int[] arr, int begin, int end, int n) {
        for (int i = end + 1; i >= begin ; i--) {
            int destination = i + n; // >>>>>>>>>>>>>>>>>>>>>>>
            if (destination < arr.length) {
                arr[destination] = arr[i];
            }
        }
    }
static void arrayCopy(int[] arr, int begin, int end, int n) {
    int destination = 0; // >>>>>>>>>>>>>>>>>
    for (int i = end + 1; i >= begin ; i--) {
        destination = i + n;
        if (destination < arr.length) {
            arr[destination] = arr[i];
        }
    }
}
public final class Solution {
    public static final void duplicateZeros(int[] arr) {
        int countZero = 0;

        for (int index = 0; index < arr.length; index++)
            if (arr[index] == 0) {
                countZero++;
            }

        int length = arr.length + countZero;

        for (int indexA = arr.length - 1, indexB = length - 1; indexA < indexB; indexA--, indexB--)
            if (arr[indexA] != 0) {
                if (indexB < arr.length) {
                    arr[indexB] = arr[indexA];
                }

            } else {
                if (indexB < arr.length) {
                    arr[indexB] = arr[indexA];
                }

                indexB--;

                if (indexB < arr.length) {
                    arr[indexB] = arr[indexA];
                }
            }
    }
}