【问题标题】:In Java, what is the fastest way to get the system time?在Java中,获取系统时间的最快方法是什么?
【发布时间】:2011-07-01 19:46:51
【问题描述】:

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

从系统获取时间的最快方法是什么?

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

【问题讨论】:

  • 你在哪里使用延迟队列的时间毫秒?
  • 我不清楚,我只在getDelay()方法中实现Delayed接口的类中使用。
  • @Renato、Calendar.getInstance() 和 new Date() 都使用 System.currentTimeMillis() 但是 Calendar 是迄今为止最慢的。

标签: java time calendar


【解决方案1】:

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

System.nanoTime().
“返回的值表示自某个固定但任意的原始时间以来的纳秒”
使用它来测量时间流逝/事件。

【讨论】:

  • 请注意,这两种方法有不同的特点,您应该选择适合您需要的一种。
  • 据我所知,DelayQueue 使用 TimeUnit.NANOSECONDS 来计算时间,所以System.nanoTime() 在这种情况下可能更合适,以避免在 getDelay() 中转换为单位`方法。
  • 你可以在那里使用任何单位,所以任何一个都可以。 nanoTime 更精确
  • 有没有人运行任何基准测试或有一些分析证据表明 System.currentTimeMillis() 比 Calendar.getInstance().getTimeInMillis() 快?
  • 即使不是,你也不应该创建一个新的实例来获取millis,当你可以从系统中获取它们时:)
【解决方案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); 等等。

【讨论】:

  • 这并没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
  • 这是 99.9 % 的推荐方式。严格来说,它可能不是最快的(我没有进行测量),但清晰度才是王道,而不是速度。
  • @craigcaulfield 我想知道为什么这不能提供问题的答案。他的回答只需要一些细节来说明为什么它是最快的。
  • 正是我对此提出的建议,并将提供统计数据。
  • 请为您的回答提供更多详细信息/解释,以提高帖子的质量。谢谢。
【解决方案3】:

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

【讨论】:

  • 是的,但我见过的所有操作系统都倾向于为你这样做
【解决方案4】:

简而言之,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

希望对你有帮助。

【讨论】:

    【解决方案5】:

    System.currentTimeMillis() 可能。

    【讨论】:

      【解决方案6】:
      System.currentTimeMillis 
      

      答案很简单

      【讨论】:

        【解决方案7】:

        long timeMilliSec = System.currentTimeMillis();

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-03-28
          • 2010-09-07
          • 2013-04-28
          • 1970-01-01
          • 2015-06-10
          • 2020-11-24
          • 2016-03-06
          相关资源
          最近更新 更多