aspose file tools*
The moose likes Beginning Java and the fly likes Computation Time of for loop. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Computation Time of for loop." Watch "Computation Time of for loop." New topic
Author

Computation Time of for loop.

kumarjit banerjee
Ranch Hand

Joined: Mar 27, 2011
Posts: 32

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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

kumarjit banerjee wrote:
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.

I don't think it matters in this case that you made the size variable final. About reducing the time this takes to run: the only way to really know is to measure it. Run the program with a profiler and measure how long it takes to run. Note that making micro-optimizations is almost never worthwile; if you want to improve the performance of a program, measure where the bottlenecks are and concentrate on improving those. Choosing better algorithms can make your program run an order of magnitude faster, while doing micro-optimization often only makes it run a few percent faster.

I'm surprised that you get an OutOfMemoryError when you use clone(). I don't know why it's doing that. 6,000,000 ints should easily fit into memory with the default memory settings for the JVM on Windows.

What exactly is your question with Case 2?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
kumarjit banerjee
Ranch Hand

Joined: Mar 27, 2011
Posts: 32

Jesper de Jong wrote:
What exactly is your question with Case 2?


I want to know that as I am facing OutOfMemoryError then instead of using clone(), do I have to set each element by using the loop or there are some other way when I only want a deep copy. Or there is some problem with my jvm and how to confirm.


Run the program with a profiler


Can you please suggest any such profiler and guide me where to find it.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Sorry, I don't have an answer about the OutOfMemoryError.

With JDK 6 you get jvisualvm, a very useful tool that you can use to check what happens with JVMs running on your computer. It can show you how much memory your program is using and also has a profiler.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Computation Time of for loop.