Hi,
The basic syntax for
for loop is
for(initialization;condition;increment){
Body
}
The initialization is computed once while entering the loop, and the condition and increment occurs at every iterations. In my application I have to use a huge number of data of the order of 6000000. In that case the time to compute is naturally large. I want to demonstrate a simple for loop for various conditions.
Case 1: Making a deep copy of array
The code in the above example as the size =1000, it is running but when the size = 6000000 the code throws Exception
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space.
So I go by the second option.
It runs fine. Please rectify my points if they are correct
1. I have made the size variable final so it will take less time.
2. I have used the clone() method when possible to reduce time.
Case 2: Making an arrray whose elements are the functions of the elements of another array
Please consider the following class Example3. In this class I have implemented that each element in the array will have the value as index position. This is just an example in general, I want to know whether my second implementation will work faster. This is because I have run the program but most of the time the second implementation is taking less time, but sometimes the reverse is also happening. The idea behind this is that, if we reduce the number of checking in the for loop, it will save computation time. The second implementation checks the value of j half number of times ie, 3000000 instead of 6000000.
I have also tried with nano seconds, there the second implementation is taking less time but, it is mentioned in the documentation that
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative).
Please help me with this code and if I am wrong somewhere