Results 1 to 7 of 7
  1. #1
    svpranay is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default time to execute a java app

    Hi,
    I would like to find the total time taken by my java program. Is there any way i could do it through eclipse ?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Do you want to include any overhead caused by eclipse?
    Do you want to include the time to load the JVM?
    What kind of app is it?

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Not sure what your asking.

    You could call System.getNanoTime() and assign it to some long variable at startup,
    then when the program is closing
    System.out.println (System.getNanoTime() - startTime);

    would give you the run time in nanoseconds (approximately).

    Why is it important?

  4. #4
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Apologies, it should be "System.nanoTime();"

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by svpranay View Post
    Hi,
    I would like to find the total time taken by my java program. Is there any way i could do it through eclipse ?
    Or, do you want to find total execution time of your application?

  6. #6
    svpranay is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default

    Hi,
    I just wanted to calculate the time taken to execute a piece of code.So i guess System.nanoTime() would work.

    Thanks for all of you.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,537
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by svpranay View Post
    Hi,
    I just wanted to calculate the time taken to execute a piece of code.So i guess System.nanoTime() would work.

    Thanks for all of you.
    You are going to be bitten by the JIT compiler if you don't pay any attention; at first a piece of code is interpreted until the HotSpot mechanism hits: then it is going to be compiled and real machine code is running. Allow your code to 'warm up' and only then set your timers; if you want to time, say, a method P(), do this:

    Java Code:
    // allow the HotSpot mechanism to do it's deeds
    for (int i= 0; i < 100000; i++)
       P();
    long start= System.nanoTime();
    for (int i= 0; i < 100000; i++)
       P();
    long t= (System.nanoTime()-start)/100000;
    The second loop takes the average runtime just to stabilize the measurements.

    kind regards,

    Jos

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. Replies: 9
    Last Post: 11-19-2009, 04:31 PM
  3. Execute external program from java
    By ankitmcgill in forum New To Java
    Replies: 1
    Last Post: 06-01-2009, 03:58 AM
  4. how to execute an executable using java
    By kala143 in forum Java 2D
    Replies: 4
    Last Post: 09-26-2008, 03:10 PM
  5. Execute a new program in java
    By mathias in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 05:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •