K
K
Kiril12212020-04-20 13:50:21
Java
Kiril1221, 2020-04-20 13:50:21

Why is my system.arraycopy implementation 30% faster, textbooks lie?

In any textbooks or articles that I read, it is written that any implementation of arrayCopy will be inferior to the native one. But here is a simple refutation, no matter how I turned the skewer with parameters, my implementation was always 25-50% faster.
Question: why in java still no one thought of changing their method to a faster and simpler one?
Tested 1000 times, bring it straight to oracle

public class TestArrayCopy{
          public static void main(String[] args) {
      
        int N=550; 
        long s;

        int SIZE=10000011;
        int[]arr1=new int[SIZE];
        int[]arr2=new int[SIZE];
        {
            for(int i=0;i<SIZE;i++){
                arr1[i]=i;arr2[i]=i;
            }
        }
 
        int[]dest1=new int[SIZE];
        int[]dest2=new int[SIZE];
        int startPos=100;
        int pos=10;
        int num=SIZE-startPos-22;
        
        s=System.currentTimeMillis();
        for(int i=0;i<N;i++)
            System.arraycopy(arr1, startPos, dest1,pos, num);
        System.out.println("System.ArrayCopy() time = "+(System.currentTimeMillis()-s));
        
        s=System.currentTimeMillis();         
        for(int i=0;i<N;i++)
            arrayCopy(arr2, startPos, dest2,pos, num); 
        System.out.println(" arrayCopy() time = "+(System.currentTimeMillis()-s));
       // System.ArrayCopy() time = 6453
      //               arrayCopy() time = 4455
 

               
    }
    
    public static void arrayCopy(Object src,int startPos,Object dest,int pos,int num){
        if(num>dist.length-pos || src.length-startPos<num){
            throw new IllegalArgumentException("...");
        if(src instanceof int[])
            arrayCopyInt((int[])src, startPos, (int[])dest, pos, num);
        else if(src instanceof double[])
            arrayCopyDouble((double[])src, startPos, (double[])dest, pos, num);
    }
    
    public static void arrayCopyInt(int src[],int startPos,int[] dist, int pos,int num){
        for(int i=pos;i<num;i++){
               dist[i]=src[startPos++];  
            }
    }
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
mayton2019, 2020-04-20
@Kiril1221

6453 ms is a suspiciously short time before the JIT compiler fires. Most likely the code is not warmed up.
I recommend the author to look at the JMH framework. It can be used to "warm up" the bytecode. This is necessary to ensure that all transients in the compiler have ended and the JVM has entered a stationary state in which it is possible to start making any measurements.
+1 on the subject of validity. The for loop is suspiciously simple. It is necessary to start the benchmark only after there is at least testing coverage. What is the use of copying that does not copy or copies incorrectly.
(looking ahead, I’ll say that I didn’t look at the code in detail. This is so. The general impression of the author’s approach)
In general, the topic of performance is very complex and delicate. I also recommend the author to watch the lectures of Shipilev and Elizarov on youtube

A
Alexey Cheremisin, 2020-04-20
@leahch

1) The code is not working. There is no arrayCopyDouble method
2) There is no check for out-of-bounds array.
3) System.arraycopy really loses on small arrays. But on the big ones, it gives a significant gain.
For example, on arrays of 83Mb - https://stackoverflow.com/questions/18638743/is-it...

D
Dmitry Alexandrov, 2020-04-20
@jamakasi666

Yes your variant is more slowly and in not idiots and in books do not lie. The question is why, yes because you are not copying anything, you are copying object references.
Draw conclusions from this, you have initialized the array, which will be slow. memory is allocated, etc. .Further your method "simply took references to ready-made data". And then a native implementation that allocates memory of the specified amount and copies data there and not links.
Well, yes, you can roll your code into a tube and sob quietly in the corner, continuing to read further, not only about the PL, but also its device.

E
Elmo Sputterspark, 2020-04-20
@Sputterspark

Bring it yourself, disgrace yourself to the end.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question