Java Cookbook-date and Times

Source: Internet
Author: User
Tags time in milliseconds

6.12 Measuring Elapsed time

--problem

You need to time how long long it takes to do something

--solution

Call System.currenttimemillis () Twice,or system.nanotime (), and subtract the first

Ressult from the second result

--discussion

The System class contains the methods for Times.currenttimemillis () return, the current time (since 1970) in Millise Conds,and Nanotime ()

(New in 1.5) returns the relative time in nanoseconds. To time some event with this:

 

Long Start=system.currenttimemillis ();  Method_to_be_timed ();  Long End=system.currenttimemillis ();  Long elapsed=end-start;//time in milliseconds Or:long start=system.nanotime ();  Method_to_be_timed ();  Long End=system.nanotime (); Long Elapsed=end-start;//time in nanoseconds

Here's a short example to measure how long it takes a user to press RETURN.

We divide the time in milliseconds by + to get seconds

and print it nicely using a NumberFormat

Long t0,t1;    System.out.println ("Press Return to Ready");  T1=system.currenttimemillis (); int b;do{b=system.in.read ();}  while (b!= ' \ r ' &&b!= ' \ n ');  T1=system.currrenttimemillis ();  Double deltat=t1-t0; System.out.println (Decimalformat.getinstance (). Format (deltat/1000.) + "seconds.");

 

Import java.io.*;  Import java.text.*;  /** * Timer for processing sqrt and I/O operations */public class timercompution{public static void Man (string[]    args) {try{new Timer (). run ();  }catch (IOException e) {System.err.println (e);} } public void Run () throws ioexception{DataInputStream n=new DataOutputStream (New Bufferedoutputstream (New File      OutputStream (Sysdep.getdevnull ()));      Long t0,t1;      System.out.println ("Java starts at" + (T0=system.currenttimemillis ()));      Double k;        for (int i=0;i<100000;i++) {k=2.1*math.sqrt ((dobule) i);      N.writedouble (k);     } System.out.println ("Java Ends at" + (T1=system.currenttimemillis ()));     Double deltat=t1-t0; System.out.println ("This run took" + decimalformat.getinstance (). Format (deltat/1000.) + "seconds.");}}

  

Java Cookbook-date and Times

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.