There are about three ways to get the millisecond value now
Java code
- Method One
- System.currenttimemillis ();
- Method Two
- Calendar.getinstance (). Gettimeinmillis ();
- Method Three
- 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
- Import Java.util.Calendar;
- Import Java.util.Date;
- Public class Timetest {
- private static long _ten_thousand=10000;
- public static void Main (string[] args) {
- long times=1000*_ten_thousand;
- long T1=system.currenttimemillis ();
- Testsystem (times);
- long T2=system.currenttimemillis ();
- System.out.println (T2-T1);
- Testcalander (times);
- long T3=system.currenttimemillis ();
- System.out.println (T3-T2);
- TestDate (times);
- long T4=system.currenttimemillis ();
- System.out.println (T4-T3);
- }
- public static void Testsystem (long times) {//use 188
- For (int i=0;i<times;i++) {
- long Currenttime=system.currenttimemillis ();
- }
- }
- public static void Testcalander (long times) {//use 6299
- For (int i=0;i<times;i++) {
- long Currenttime=calendar.getinstance (). Gettimeinmillis ();
- }
- }
- public static void TestDate (long times) {
- For (int i=0;i<times;i++) {
- long currenttime=new Date (). GetTime ();
- }
- }
- }
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
- 187
- 7032
- 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