Java gets timestamp, which is faster

Source: Internet
Author: User

There are about three ways to get the millisecond value now

Java code
    1. Method One
    2. System.currenttimemillis ();
    3. Method Two
    4. Calendar.getinstance (). Gettimeinmillis ();
    5. Method Three
    6. New Date (). GetTime ();

Recently the monitoring system, found the first two methods in the code, and then suddenly have an idea, in the end which is faster?

Then the following experiment was done:

Java code
  1. Import Java.util.Calendar;
  2. Import Java.util.Date;
  3. Public class Timetest {
  4. private static long _ten_thousand=10000;
  5. public static void Main (string[] args) {
  6. long times=1000*_ten_thousand;
  7. long T1=system.currenttimemillis ();
  8. Testsystem (times);
  9. long T2=system.currenttimemillis ();
  10. System.out.println (T2-T1);
  11. Testcalander (times);
  12. long T3=system.currenttimemillis ();
  13. System.out.println (T3-T2);
  14. TestDate (times);
  15. long T4=system.currenttimemillis ();
  16. System.out.println (T4-T3);
  17. }
  18. public static void Testsystem (long times) {//use 188
  19. For (int i=0;i<times;i++) {
  20. long Currenttime=system.currenttimemillis ();
  21. }
  22. }
  23. public static void Testcalander (long times) {//use 6299
  24. For (int i=0;i<times;i++) {
  25. long Currenttime=calendar.getinstance (). Gettimeinmillis ();
  26. }
  27. }
  28. public static void TestDate (long times) {
  29. For (int i=0;i<times;i++) {
  30. long currenttime=new Date (). GetTime ();
  31. }
  32. }
  33. }

Because it's simple, I don't annotate, each method runs 10 million times and then looks at the results of the run.

Java code
    1. 187
    2. 7032
    3. 297

It turns out that System.currenttimemillis () is the fastest

Calendar.getinstance (). Gettimeinmillis () This is the slowest way to look at the source code will find that Canlendar because it takes a lot of time to deal with time zone problems.

So it is recommended to use the first method more.

In addition, there are many efficient methods in the System class, such as ArrayCopy.

http://tangmingjie2009.iteye.com/blog/1543166

Java gets timestamp, which is faster

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.