简体   繁体   English

Java,简单程序的执行时间太长

[英]Java, execution time of a simple program too high

Good morning everybody, i'm trying to write a method that given a N number will create a spiral matrix with size of N*N, the method below works just fine but the execution time of the code is about 2s when it should be around few ms, i don't know what actually makes the code so slow, can you help me figure it out?, thanks. 大家早上好,我正在尝试编写一个方法,给定一个N数字将创建一个大小为N * N的螺旋矩阵,下面的方法工作得很好,但是代码的执行时间大约是2s几毫秒,我不知道实际上是什么使代码如此缓慢,您能帮我弄清楚吗?,谢谢。

I wrote another method that called this one (spirale) with different numbers in the following order and had the following results. 我编写了另一种方法,该方法按以下顺序使用不同的编号调用此方法(螺旋式),并得到以下结果。

test1: n=10 execution time-> 2475ms
test2: n=5 execution time-> 7795ms
test3: n=1 execution time-> 7169ms
test4: n=0 execution time-> 7034ms
test5: n=6 execution time-> 7056ms



    public static int[][] spirale(int N){
    int i=0, j=0, giro = 0, numbers = 1;
    int mat[][] = new int[N][N];
    while(true){
        for(j=0+giro; j<N-giro; j++){
            if(numbers>N*N) return mat;
            mat[i][j] = numbers++;
        }
        j--;
        for(i=1+giro; i<N-giro ; i++){
            if(numbers>N*N) return mat;
            mat[i][j] = numbers++;
        }
        i--;
        for(j=N-2-giro; j>=0+giro; j--){
            if(numbers>N*N) return mat;
            mat[i][j] = numbers++;
        }
        j++;
        for(i=N-2-giro ; i>0+giro ; i--){
            if(numbers>N*N) return mat;
            mat[i][j] = numbers++;
        }
        i++;
        giro++;
    }

}

In general this: 一般而言:

test4: n=0 execution time-> 7034ms

suggests that the problem not in the spirale method since it will return immidiately in first iteration here: 建议该问题不在spirale方法中,因为它将在此处的第一次迭代中立即返回:

if(numbers>N*N) return mat;

I guess you should look in your code where you measure it (you have to measure it in code, not just the program execution time due to the process creation overhead) 我猜您应该在代码中对其进行测量(由于过程创建的开销,您必须在代码中进行测量,而不仅是程序执行时间)

Anyway, this is not important for the small N that you use, but you need to calculate N*N only once. 无论如何,这对于您使用的小N并不重要,但是您只需要计算一次N * N。 I guess the compiler optimization will do the work for you though. 我想编译器优化将为您完成这项工作。

I mean: 我的意思是:

int squareN = N*N;
 while(true){

        for(j=0+giro; j<N-giro; j++){
            if(numbers>squareN ) return mat;
            mat[i][j] = numbers++;
        }
        j--;
        for(i=1+giro; i<N-giro ; i++){
            if(numbers>squareN ) return mat;
            mat[i][j] = numbers++;
        }
        i--;
        for(j=N-2-giro; j>=0+giro; j--){
            if(numbers>squareN ) return mat;
            mat[i][j] = numbers++;
        }
        j++;
        for(i=N-2-giro ; i>0+giro ; i--){
            if(numbers>squareN ) return mat;
            mat[i][j] = numbers++;
        }
        i++;
        giro++;
    }

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

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