File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes get elapsed time.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "get elapsed time...." Watch "get elapsed time...." New topic

get elapsed time....

Miguel Enriquez
Ranch Hand

Joined: Mar 13, 2004
Posts: 95
here my code:

public class timeelap
public static void main(String[] args)
long start_time, end_time;
for (int i=0; i<10000;i++)
System.out.println("Hello World\n");
long timeelapsed = (end_time - start_time);
long milliseconds = timeelapsed % 1000;
long seconds = (timeelapsed % 1000) % 60;
long minutes = (timeelapsed % 60000) % 60;

System.out.println("Time Elapsed: "+ timeelapsed+", Start Time:"+start_time+", end Time: "+end_time+" ("+minutes+":"+seconds+":"+milliseconds+" )");

the dispaly at the end are:

Hello World

Hello World

Hello World

Time Elapsed: 3703, Start Time:1127853771781, end Time: 1127853775484 (43:43:703

not like me the result, so so the elapsed time are: minor than 1 second...

Henry Wong

Joined: Sep 28, 2004
Posts: 19344

Your calculations for second and minutes are incorrect. I think you used the mod operator, where you should have used division.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
long mark = System.nanoTime();

... do something ...

long seconds = TimeUnit.SECONDS.convert(mark - System.nanoTime(), TimeUnit.NANOSECONDS);

I should mention: Java 5.0 using java.util.concurrent package's TimeUnit.
[ September 27, 2005: Message edited by: Rick O'Shay ]
I agree. Here's the link:
subject: get elapsed time....