• 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

Finding the time taken for program to execute

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was wondering if there are any built in features and/or a possible code implementations that can show the time a Java Eclipse program has taken to run.
I have seen the below plugin being used however i'm told it has been discontinued:



Thank you in advance for any time taken to help.
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no such thing as an Eclipse program, only Java® programs. If you mean you want to get Eclipse to time from starting to run the program until the JVM exits, try looking at this SO thread. It mentions the TPTP project which has been archived, and probably not removed. It also says you can add time to a run configuration, but I couldn't work out where. It didn't run correctly when I tried it.
 
Harry Orson
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After some further research I came across this solution which solved my problem:

At the first line of your main method add:

Then at the last line of the main method add;


For those who prefer a time in milliseconds (including myself) I changed the code that goes on the last line of your main method to this to this;

 
Harry Orson
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:There is no such thing as an Eclipse program, only Java® programs. If you mean you want to get Eclipse to time from starting to run the program until the JVM exits, try looking at this SO thread. It mentions the TPTP project which has been archived, and probably not removed. It also says you can add time to a run configuration, but I couldn't work out where. It didn't run correctly when I tried it.


Thank you for your corrections, noted. I meant to say a Java program written within Eclipse, but yes what you then mentioned is what i was looking for. Thanks.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try searching for Java Profilers and Benchmarking Java programs. You might find something useful.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harry Orson wrote:. . . System.nanoTime();
. . .
milliseconds = duration / 1000000.0 . . .

Use integer arithmetic rather than doubles. I would prefer a slightly more complicated algorithm that that suggested by the System class documentation:-That allows you to estimate and eliminate the time for a nanoTime() call, but that might be an insignificant duration if foo() takes a long time. If you prefer milliseconds, don't divide by 1000000, but use this instead. A millisecond timing will be very inaccurate if foo() takes a short time to complete.

That is of course a completely different timing from how long it takes Eclipse to run your application.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My brain must be fried... I don't get how that formula adjusts for the call to nanoTime(). Moreover,  start * 2 would be evaluated before either of those other terms, which makes me even more confused. Are there some parentheses missing in that expression?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


That's the same as



Which takes the obvious calculation (after - start) and subtracts off the time to execute System.nanoTime().

But for me if you're running something for which the time to execute System.nanoTime() isn't a negligible quantity, then you're ignoring all of the problems which arise when you do a micro-benchmark in Java. So I wouldn't bother with that myself.

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I'm caught up now.  Assuming the call to nano() costs the same # of ns every time,

Got it. Thanks.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . .  Assuming the call to nano() costs the same # of ns every time . . .

My technique does require quite a lot of assumptions like that.

Try this sort of code several times:-. . . and the nanoTime call seems similar in duration every time, and about 0.001 of the println() call, so it reduces the time from 100% to 99.9%
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic