简体   繁体   English

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

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

I'm developing a system that often use the system time because the Delayed interface.我正在开发一个经常使用系统时间的系统,因为Delayed接口。

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

System.currentTimeMillis()
"Returns the current time in milliseconds" . “以毫秒为单位返回当前时间”
Use this to get the actual system time.使用它来获取实际的系统时间。

System.nanoTime() . System.nanoTime()
"The value returned represents nanoseconds since some fixed but arbitrary origin time" “返回的值表示自某个固定但任意的原始时间以来的纳秒”
Use this is you're measuring time lapses / events.使用它是您正在测量时间流逝/事件。

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.对于较大的请求数,我认为应该有一个线程负责更新当前系统时间,避免每个线程独立执行。

In short answer, System.currentTimeMillis() is faster.简而言之, 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);
}

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.我希望这能帮到您。

System.currentTimeMillis() is fastest as per below test case 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);
    }
}

Output: 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()更快+更简单,并提供其他实用程序以及Instant.now().getEpochSecond(); , Instant.now().getNano(); , Instant.now().getNano(); , Instant.now().compareTo(otherInstant); , Instant.now().compareTo(otherInstant); and many more.还有很多。

long timeMilliSec = System.currentTimeMillis();

System.currentTimeMillis() probably. System.currentTimeMillis()可能。

System.currentTimeMillis 

is the simple answer是简单的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Java 中的 System.in 读取的最快方法是什么? - What's the fastest way to read from System.in in Java? 在 Java 中获取 nanos unix 纪元时间的最快方法 - Fastest way to get nanos unix epoch time in Java 在Java项目中获取所有构建错误的最快方法是什么? - What's the fastest way to get all build errors in a Java project? 使用 Java 获取唯一文件哈希的最快方法是什么? - What is the fastest way to get unique file hash using Java? 概念:让 Java 程序运行的外生因素的最快方法是什么? - Conceptual: what is the fastest way to get exogenous factors for a Java program to run? 使用Java获取HTML内容的最快方法是什么? - What is the fastest way to get a HTML Content using java? 在Java中获取k个最小(或最大)数组元素的最快方法是什么? - What is the fastest way to get k smallest (or largest) elements of array in Java? 什么是最快的方法来获取csv文件在Java中的尺寸 - what is the fastest way to get dimensions of a csv file in java Java中获取数量的最快因素的最快方法是什么 - What is the fastest way in Java to get the amount of factors a number has 从Java应用程序获取数据到Cassandra 2的最快方法是什么? - What is the fastest way to get data into Cassandra 2 from a Java application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM