Performance Tip: System.arrayCopy is usually faster than a manual array copy.

Overview

Using System.arrayCopy can be cumbersome, but it has some performance advantages.

Replacing a manual array copy


From BigInteger.shiftLeft()
for (int i=0; i < magLen; i++)
    newMag[i] = mag[i];
Can be replaced with
System.arrayCopy(mag, 0, newMag, 0, magLen);

Bonus tip: Replace a manual clone with .clone() for primitive array or arrays of immutables

When a primitive array is manually copied and is created the same length, it worth using clone(). The clone() should usually be avoided due to some of its design limitations however, for primitive arrays and arrays of immutables it is the best option. From Introspector.getBeanInfoSearchPath
String result[] = new String[searchPath.length];
for (int i = 0; i < searchPath.length; i++) {
    result[i] = searchPath[i];
}
return result;
Can be replaced with
return searchPath.clone();

Comments

Popular posts from this blog

Low Latency Microservices, A Retrospective

Unusual Java: StackTrace Extends Throwable

System wide unique nanosecond timestamps