Java Tutorial Source Code: The System Class
Here's a demo on some of these methods.
import java.io.*;
class SystemDemo {
public static void main(String args[]) throws IOException {
int arr1[] = new int[5000000];
int arr2[] = new int[5000000];
long startTime, endTime;
/* initialize arr1 */
for (int i = 0; i < arr1.length; i++) {
arr1[i] = i + 1;
}
/* copying manually */
startTime = System.currentTimeMillis();
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
endTime = System.currentTimeMillis();
System.out.println("Time for manual copy: " +
(endTime-startTime) + " ms.");
/* using the copy utility provided by java – the
arraycopy method */
startTime = System.currentTimeMillis();
System.arraycopy(arr1, 0, arr2, 0, arr1.length);
endTime = System.currentTimeMillis();
System.out.println("Time w/the use of arraycopy: " + (endTime-startTime) + " ms.");
System.gc(); //force garbage collector to work
System.setIn(new FileInputStream("temp.txt"));
/* in NetBeans, temp.txt should be located
in the project folder */
System.exit(0);
}
}
The program gives varying output. It may generate the following sample output:
Time for manual copy: 32 ms.
Time w/the use of arraycopy: 31 ms.
Java Tutorial Source Code: The System Class
Java Tutorial Source Code: The System Class
Java Tutorial Source Code: The System Class
Java Tutorial Source Code: The System Class
Java Tutorial Source Code: The System Class
Here's a demo on some of these methods.
import java.io.*;
class SystemDemo {
public static void main(String args[]) throws IOException {
int arr1[] = new int[5000000];
int arr2[] = new int[5000000];
long startTime, endTime;
/* initialize arr1 */
for (int i = 0; i < arr1.length; i++) {
arr1[i] = i + 1;
}
/* copying manually */
startTime = System.currentTimeMillis();
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
endTime = System.currentTimeMillis();
System.out.println("Time for manual copy: " +
(endTime-startTime) + " ms.");
/* using the copy utility provided by java – the
arraycopy method */
startTime = System.currentTimeMillis();
System.arraycopy(arr1, 0, arr2, 0, arr1.length);
endTime = System.currentTimeMillis();
System.out.println("Time w/the use of arraycopy: " + (endTime-startTime) + " ms.");
System.gc(); //force garbage collector to work
System.setIn(new FileInputStream("temp.txt"));
/* in NetBeans, temp.txt should be located
in the project folder */
System.exit(0);
}
}
The program gives varying output. It may generate the following sample output:
Time for manual copy: 32 ms.
Time w/the use of arraycopy: 31 ms.
Java Tutorial Source Code: The System Class
Java Tutorial Source Code: The System Class
Java Tutorial Source Code: The System Class
Java Tutorial Source Code: The System Class
Java Tutorial Source Code: The System Class



