java - Why clone() is the best way for copying arrays? -


it's shame me, did not know that:

you should use clone copy arrays, because that's fastest way it.

as josh bloch states in blog: http://www.artima.com/intv/bloch13.html

i used system.arraycopy(...). both approaches native, without going deeper sources of libraries can not figure out, why so.

my question simple: why fastest way? what difference system.arraycopy? difference explained here, not answer question why josh bloch considers clone() fastest way.

i make points why clone() fastest way copy array system.arraycopy(..) or others:

1. clone() doesn't have typechecking before copying source array destination 1 provided here. simple allocates new memory space , assigns objects it. on other hand, system.arraycopy(..) checks type , copies array.

2. clone() breaks optimization eliminate redundant zeroing. know, every allocated array in java must initialized 0s or respective default values. however, jit can avoid zeroing array if sees array filled right after creation. makes faster compared changing copy values existing 0s or respective default values. while using system.arraycopy(..) spends significant amount of time clearing , copying initialized array. have performed of benchmark tests.

@benchmarkmode(mode.throughput) @fork(1) @state(scope.thread) @warmup(iterations = 10, time = 1, batchsize = 1000) @measurement(iterations = 10, time = 1, batchsize = 1000) public class benchmarktests {      @param({"1000","100","10","5", "1"})     private int size;     private int[] original;      @setup     public void setup() {         original = new int[size];         (int = 0; < size; i++) {             original[i] = i;         }     }      @benchmark     public int[] systemarraycopy() {         final int length = size;         int[] destination = new int[length];         system.arraycopy(original, 0, destination, 0, length);         return destination;     }       @benchmark     public int[] arrayclone() {         return original.clone();     }  } 

output:

benchmark                        (size)   mode  cnt       score      error  units arraycopy.systemarraycopy            1  thrpt   10   26324.251 ± 1532.265  ops/s arraycopy.systemarraycopy            5  thrpt   10   26435.562 ± 2537.114  ops/s arraycopy.systemarraycopy           10  thrpt   10   27262.200 ± 2145.334  ops/s arraycopy.systemarraycopy          100  thrpt   10   10524.117 ±  474.325  ops/s arraycopy.systemarraycopy         1000  thrpt   10     984.213 ±  121.934  ops/s arraycopy.arrayclone                 1  thrpt   10   55832.672 ± 4521.112  ops/s arraycopy.arrayclone                 5  thrpt   10   48174.496 ± 2728.928  ops/s arraycopy.arrayclone                10  thrpt   10   46267.482 ± 4641.747  ops/s arraycopy.arrayclone               100  thrpt   10   19837.480 ±  364.156  ops/s arraycopy.arrayclone              1000  thrpt   10    1841.145 ±  110.322  ops/s 

according outputs getting clone twice fast system.arraycopy(..)

3. also, using manual copying method clone() results faster ouput because doesn't have make vm calls (unlike system.arraycopy()).


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -