• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Computation Time of for loop.

 
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic