OPT.IRB
Use `System.arraycopy ()' instead of using a loop to copy an array
Description
This rule flags code where a loop is used to copy arrays.
`System.arraycopy ()' is much faster that using a loop to copy an array.
Example
package OPT;
public class IRB
{
void method () {
int[] array1 = new int [100];
for (int i = 0; i < array1.length; i++) {
array1 [i] = i;
}
int[] array2 = new int [100];
for (int i = 0; i < array2.length; i++) {
array2 [i] = array1 [i]; // Violation
}
}
}
Repair
package OPT;
public class IRB
{
void method () {
int[] array1 = new int [100];
for (int i = 0; i < array1.length; i++) {
array1 [i] = i;
}
int[] array2 = new int [100];
System.arraycopy(array1, 0, array2, 0, 100);
}
}
Reference
http://www.cs.cmu.edu/~jch/java/speed.html
|