• 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

get elapsed time....

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here my code:

public class timeelap
{
public static void main(String[] args)
{
long start_time, end_time;
start_time=System.currentTimeMillis();
for (int i=0; i<10000;i++)
{
System.out.println("Hello World\n");
}
end_time=System.currentTimeMillis();
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...

thanks
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

Henry
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic