English 中文(简体)
我有一个 Java业绩问题,我不理解
原标题:I have a Java performance issue that I don t understand

我撰写了一部法典,以建立一个多层面阵列,而不是一系列阵列,使我能够拯救一些记忆。 然后,我进行了一些测试,以将其速度与定期的 Java阵列(int [])相提并论,因为我不希望我的方案即使能节省一些记忆也更慢。 我在时间测试中看到的情况使我感到困惑。 这里是试验运行的典型结果。 时间相同。 通知后,最后两条比前四大得多。

time: 58343722 ns
time: 59451156 ns
time: 51374777 ns

time: 61777424 ns
time: 813156695 ns
time: 782140511 ns

现在,我认为第一件事是垃圾收集器打什么。 我将记忆限制提高到5GB(Xmx5g),以便垃圾收集者不敢开车。 无改动。 我把事情推向周围,但模式仍然存在。

因此,情况如何? 头三次,守则的范围是职能,我三次称呼。 在后三倍的时间里,在单一功能中重复使用代码。 因此,这种模式是,如果守则的范围在同一职能中是多次运作的,则从第二行的法典开始,然后继续适用。

我确实发现一种改变,将产生这样的结果:

time: 58729424 ns
time: 59965426 ns
time: 51441618 ns

time: 57359741 ns
time: 65362705 ns
time: 857942387 ns

我在第二三条码轨道之间增加了一毫秒的延迟。 仅仅加快了区块第二位代码轨道,而且不出现任何拖延,就会加快那里的任何法规范围。

Frankly, I m confused. I can t explain this behavior. Can someone shed some light on what is going on?

Here is the code:

package multidimensionalarraytests;

import java.lang.reflect.Array;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MultidimensionalArrayTests {
    static ArrayInt2Dv1 array=new ArrayInt2Dv1(10000,10000);

    public static void main(String[] args) {
        System.out.println("ignore the warmup");
        test();
        test();
        combined();
        combined();
        
        System.out.println("running tests");
        test();
        test();
        test();
        System.out.println();
        combined();
    }

    static long test(){
        int value=1;
        long start,stop,time;
        
        System.out.print("time: ");
        start=System.nanoTime();
        for(int x=0;x<array.length1;x++){
            for(int y=0;y<array.length2;y++){
                array.set(x, y, value);
                value=array.get(x, y);
            }   
        }
        stop=System.nanoTime();
        time=(stop-start);
        System.out.println(time+" ns");
        return time;
    }

    static void combined(){
        int value=1;
        long start,stop,time;
        
        System.out.print("time: ");
        start=System.nanoTime();
        for(int x=0;x<array.length1;x++){
            for(int y=0;y<array.length2;y++){
                array.set(x, y, value);
                value=array.get(x, y);
            }   
        }
        stop=System.nanoTime();
        time=(stop-start);
        System.out.println(time+" ns");

        //try {Thread.sleep(1);} catch (InterruptedException ex) {}

        System.out.print("time: ");
        start=System.nanoTime();
        for(int x=0;x<array.length1;x++){
            for(int y=0;y<array.length2;y++){
                array.set(x, y, value);
                value=array.get(x, y);
            }   
        }
        stop=System.nanoTime();
        time=(stop-start);
        System.out.println(time+" ns");

        //try {Thread.sleep(60000);} catch (InterruptedException ex) {}

        System.out.print("time: ");
        start=System.nanoTime();
        for(int x=0;x<array.length1;x++){
            for(int y=0;y<array.length2;y++){
                array.set(x, y, value);
                value=array.get(x, y);
            }   
        }
        stop=System.nanoTime();
        time=(stop-start);
        System.out.println(time+" ns");     
    }
}

并且

package multidimensionalarraytests;

public class ArrayInt2Dv1 {
    int [] array;

    public final int length1;
    public final int length2;

    public ArrayInt2Dv1(int length1, int length2){
        this.length1=length1;
        this.length2=length2;
        array=new int[length1*length2];
    }

    public int get(int x,int y){
        return array[x*length2+y];
    }

    public void set(int x,int y,int value){
        array[x*length2+y]=value;
    }
}

纽约总部

视窗7的产出包括:Xms5g -Xmx5g -XX:+PrintCompilation -verbose:gc -XX:CICompilerCount=1-Xbatch

time:     299    1    b        multidimensionalarraytests.ArrayInt2Dv1::set (15 bytes)
    302    2    b        multidimensionalarraytests.ArrayInt2Dv1::get (14 bytes)
    303    1 %  b        multidimensionalarraytests.MultidimensionalArrayTests::test @ 31 (114 bytes)
    358    1 %           multidimensionalarraytests.MultidimensionalArrayTests::test @ -2 (114 bytes)   made not entrant
60671451 ns
    359    3    b        multidimensionalarraytests.MultidimensionalArrayTests::test (114 bytes)
time:     365    2 %  b        multidimensionalarraytests.MultidimensionalArrayTests::test @ 31 (114 bytes)
58104484 ns
time:     425    3 %  b        multidimensionalarraytests.MultidimensionalArrayTests::combined @ 31 (330 bytes)
69008251 ns
time: 806898159 ns
time: 845447596 ns
   2146    4    b        multidimensionalarraytests.MultidimensionalArrayTests::combined (330 bytes)
time: 52493169 ns
time: 804304528 ns
time: 845500191 ns
running tests
time: 51290771 ns
time: 51922285 ns
time: 51264108 ns

time: 52258679 ns
time: 842229025 ns
time: 871403625 ns

On Linux (Ubuntu on VirtualBox on the same machine) with the same options:

    283   1   b   java.lang.String::hashCode (60 bytes)
    285   2   b   sun.nio.cs.UTF_8$Encoder::encodeArrayLoop (490 bytes)
    287   3   b   java.lang.String::charAt (33 bytes)
    287   4   b   java.lang.String::indexOf (151 bytes)
    297   5   b   java.io.UnixFileSystem::normalize (75 bytes)
   2850   6   b   java.lang.String::lastIndexOf (156 bytes)
ignore the warmup
time:    5885   7   b   multidimensionalarraytests.ArrayInt2Dv1::set (15 bytes)
   5948   8   b   multidimensionalarraytests.ArrayInt2Dv1::get (14 bytes)
   5949   1%  b   multidimensionalarraytests.MultidimensionalArrayTests::test @ 31 (114 bytes)
11529998483 ns
  17565   9   b   multidimensionalarraytests.MultidimensionalArrayTests::test (114 bytes)
time: 1619622928 ns
time:   19718   2%  b   multidimensionalarraytests.MultidimensionalArrayTests::combined @ 31 (330 bytes)
475786382 ns
time: 288586857 ns
time: 315560700 ns
  20789  10   b   multidimensionalarraytests.MultidimensionalArrayTests::combined (330 bytes)
time: 460577230 ns
time: 311525066 ns
time: 312343429 ns
running tests
time: 310261854 ns
time: 298826592 ns
time: 304689920 ns

time: 315416579 ns
time: 299473245 ns
time: 290211350 ns
最佳回答

Try -XX:+PrintCompilation This should show that the whole method is optimised after the first loop iterates 10000 times. The problem is that the second/thirds loop is optimised with no statistical/counter information. Sometimes this doesn t matter, sometimes the later loops are much slower and if you swap the order of the loops, the later loops improve and the first loop gets slower.

The simple way to fix this is to place each loop in its own method and each loop will be optimised properly.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签