在Java中,获取系统时间的最快方法是什么?

时间:2011-07-01 19:46:51

标签: java time calendar

我正在开发一个经常使用系统时间的系统,因为Delayed接口。

什么是从系统中获取时间的紧固方式?

目前我每次需要时间时都会使用Calendar.getInstance().getTimeInMillis(),但我不知道是否有更快的方法。

7 个答案:

答案 0 :(得分:17)

System.currentTimeMillis()
“以毫秒为单位返回当前时间” 使用它来获取实际的系统时间。

System.nanoTime()
“返回的值表示纳秒,因为某些固定但任意的原始时间”
使用此功能,您需要测量时间流逝/事件。

答案 1 :(得分:2)

根据以下测试案例,

System.currentTimeMillis()最快

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);
    }
}

输出:

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

但是

Instant.now()快速且简单,并提供其他实用程序,例如Instant.now().getEpochSecond();Instant.now().getNano();Instant.now().compareTo(otherInstant);等。

答案 2 :(得分:1)

System.currentTimeMillis()可能。

答案 3 :(得分:1)

System.currentTimeMillis 

是简单的答案

答案 4 :(得分:1)

long timeMilliSec = System.currentTimeMillis();

答案 5 :(得分:1)

对于大请求号,我认为应该有一个线程负责更新当前系统时间,避免每个线程独立完成。

答案 6 :(得分:1)

简而言之,System.currentTimeMillis()更快。

@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);
}

我跑过测试用例,发现以下结果:

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

我希望,这会对你有帮助。