• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding the time taken for program to execute  RSS feed

 
Harry Orson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 12
  • Mark post as helpful
  • send pies
  • 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: 12
  • Mark post as helpful
  • send pies
  • 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.
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try searching for Java Profilers and Benchmarking Java programs. You might find something useful.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • 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: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Clapham
Sheriff
Posts: 22526
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 55793
164
  • Mark post as helpful
  • send pies
  • 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%
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!