In Java, what is the fastest way to get the system time?

放肆的年华 提交于 2021-02-18 10:46:41

问题


I'm developing a system that often use the system time because the Delayed interface.

What is fastet way to get the time from system?

Currently I'm using Calendar.getInstance().getTimeInMillis() every time I need to get the time, but I don't know if there is a faster way.


回答1:


System.currentTimeMillis()
"Returns the current time in milliseconds".
Use this to get the actual system time.

System.nanoTime().
"The value returned represents nanoseconds since some fixed but arbitrary origin time"
Use this is you're measuring time lapses / events.




回答2:


For large request number, I believe there should be a Thread in charge of update the current system time avoing each thread doing it independently.




回答3:


In short answer, System.currentTimeMillis() is faster.

@Test
public void testSystemCurrentTime() {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    for (int i = 0; i < 1_00_000; i++) {
        System.currentTimeMillis();
    }
    stopwatch.stop();
    System.out.println("System.currentTimeMillis(): " + stopwatch);
}

@Test
public void testDateTime() {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    for (int i = 0; i < 1_00_000; i++) {
        (new Date()).getTime();
    }
    stopwatch.stop();
    System.out.println("(new Date()).getTime(): " + stopwatch);
}

@Test
public void testCalendarTime() {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    for (int i = 0; i < 1_00_000; i++) {
        Calendar.getInstance().getTimeInMillis();
    }
    stopwatch.stop();
    System.out.println("Calendar.getInstance().getTimeInMillis(): " + stopwatch);
}

I ran above test cases and I found following result:

System.currentTimeMillis(): 5.208 ms    (new Date()).getTime(): 19.57 ms    Calendar.getInstance().getTimeInMillis(): 148.2 ms
System.currentTimeMillis(): 4.685 ms    (new Date()).getTime(): 11.53 ms    Calendar.getInstance().getTimeInMillis(): 122.6 ms
System.currentTimeMillis(): 4.734 ms    (new Date()).getTime(): 11.66 ms    Calendar.getInstance().getTimeInMillis(): 131.5 ms
System.currentTimeMillis(): 4.018 ms    (new Date()).getTime(): 19.33 ms    Calendar.getInstance().getTimeInMillis(): 127.6 ms
System.currentTimeMillis(): 5.474 ms    (new Date()).getTime(): 16.74 ms    Calendar.getInstance().getTimeInMillis(): 113.6 ms
System.currentTimeMillis(): 3.871 ms    (new Date()).getTime(): 14.46 ms    Calendar.getInstance().getTimeInMillis(): 120.5 ms
System.currentTimeMillis(): 8.223 ms    (new Date()).getTime(): 11.65 ms    Calendar.getInstance().getTimeInMillis(): 173.8 ms
System.currentTimeMillis(): 4.611 ms    (new Date()).getTime(): 9.978 ms    Calendar.getInstance().getTimeInMillis(): 117.9 ms
System.currentTimeMillis(): 3.794 ms    (new Date()).getTime(): 11.33 ms    Calendar.getInstance().getTimeInMillis(): 89.79 ms
System.currentTimeMillis(): 4.298 ms    (new Date()).getTime(): 12.37 ms    Calendar.getInstance().getTimeInMillis(): 123.8 ms

I hope, this will help you.




回答4:


System.currentTimeMillis() is fastest as per below test case

public class ClassTest
{
    @Test
    public void testSystemCurrentTime()
    {
        final Stopwatch stopwatch = Stopwatch.createStarted();
        for (int i = 0; i < 1_00_000; i++)
        {
            System.currentTimeMillis();
        }
        stopwatch.stop();
        System.out.println("System.currentTimeMillis(): " + stopwatch);
    }

    @Test
    public void testDateTime()
    {
        final Stopwatch stopwatch = Stopwatch.createStarted();
        for (int i = 0; i < 1_00_000; i++)
        {
            (new Date()).getTime();
        }
        stopwatch.stop();
        System.out.println("(new Date()).getTime(): " + stopwatch);
    }

    @Test
    public void testCalendarTime()
    {
        final Stopwatch stopwatch = Stopwatch.createStarted();
        for (int i = 0; i < 1_00_000; i++)
        {
            Calendar.getInstance().getTimeInMillis();
        }
        stopwatch.stop();
        System.out.println("Calendar.getInstance().getTimeInMillis(): " + stopwatch);
    }

    @Test
    public void testInstantNow()
    {
        final Stopwatch stopwatch = Stopwatch.createStarted();
        for (int i = 0; i < 1_00_000; i++)
        {
            Instant.now();
        }
        stopwatch.stop();
        System.out.println("Instant.now(): " + stopwatch);
    }
}

Output:

(new Date()).getTime(): 36.89 ms
Calendar.getInstance().getTimeInMillis(): 448.0 ms
Instant.now(): 34.13 ms
System.currentTimeMillis(): 10.28 ms

But,

Instant.now() is fast + simpler and provides other utility as well like Instant.now().getEpochSecond();,Instant.now().getNano();, Instant.now().compareTo(otherInstant); and many more.




回答5:


System.currentTimeMillis() probably.




回答6:


System.currentTimeMillis 

is the simple answer




回答7:


long timeMilliSec = System.currentTimeMillis();



来源:https://stackoverflow.com/questions/6552575/in-java-what-is-the-fastest-way-to-get-the-system-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!