System.nanoTime()和System.currentTimeMillis()区别 (还是直接看API比较清晰)
来源:互联网 发布:java 线程池原理 编辑:程序博客网 时间:2024/05/16 05:46
先来看看System的静态方法nanoTime()
public static long nanoTime()
- 返回最准确的可用系统计时器的当前值,以毫微秒为单位。
此方法只能用于测量已过的时间,与系统或钟表时间的其他任何时间概念无关。返回值表示从某一固定但任意的时间算起的毫微秒数(或许从以后算起,所以该值可能为负)。此方法提供毫微秒的精度,但不是必要的毫微秒的准确度。它对于值的更改频率没有作出保证。在取值范围大于约 292 年(263 毫微秒)的连续调用的不同点在于:由于数字溢出,将无法准确计算已过的时间。
例如,测试某些代码执行的时间长度:
long startTime = System.nanoTime(); // ... the code being measured ... long estimatedTime = System.nanoTime() - startTime;
- 返回:
- 系统计时器的当前值,以毫微秒为单位。
- 从以下版本开始:
- 1.5
currentTimeMillis
public static long currentTimeMillis()
- 返回以毫秒为单位的当前时间。注意,当返回值的时间单位是毫秒时,值的粒度取决于底层操作系统,并且粒度可能更大。例如,许多操作系统以几十毫秒为单位测量时间。
请参阅
Date
类的描述,了解可能发生在“计算机时间”和协调世界时(UTC)之间的细微差异的讨论。 - 返回:
- 当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫秒为单位测量)。
- 另请参见:
Date
public static void main(String[] args) { //首先我们创建一个大的list,里面的元素都是唯一的: int max = 1000000; List<String> values = new ArrayList<>(max); for (int i = 0; i < max; i++) { UUID uuid = UUID.randomUUID(); values.add(uuid.toString()); } //我们测量一下对这个集合进行排序所使用的时间。 //顺序排序 long t0 = System.nanoTime(); long count = values.stream().sorted().count(); System.out.println(count); long t1 = System.nanoTime(); long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0); System.out.println(String.format("sequential sort took: %d ms", millis)); //并行排序 long tt0 = System.nanoTime(); long countt = values.parallelStream().sorted().count(); System.out.println(countt); long tt1 = System.nanoTime(); long milliss = TimeUnit.NANOSECONDS.toMillis(tt1 - tt0); System.out.println(String.format("parallel sort took: %d ms", milliss));}
0 0
- System.nanoTime()和System.currentTimeMillis()区别 (还是直接看API比较清晰)
- System.currentTimeMillis和System.nanoTime的区别
- System.currentTimeMillis() 和 System.nanoTime();
- system.nanoTime()和system.currentTimeMillis()
- System.nanoTime与System.currentTimeMillis比较
- java中System.currentTimeMillis()和System.nanoTime()区别1
- java的System.currentTimeMillis()和System.nanoTime()有什么区别?
- System.nanoTime与System.currentTimeMillis的区别
- System.nanoTime与System.currentTimeMillis的区别
- System.nanoTime与System.currentTimeMillis的区别
- System.nanoTime与System.currentTimeMillis的区别
- System.nanoTime与System.currentTimeMillis的区别
- System.nanoTime与System.currentTimeMillis的区别
- System.nanoTime与System.currentTimeMillis的区别
- System.nanoTime与System.currentTimeMillis的区别
- System.nanoTime与System.currentTimeMillis的区别
- System.nanoTime与System.currentTimeMillis的区别
- System.nanoTime与System.currentTimeMillis的区别
- Leetcode 210. Course Schedule II
- Android bluetooth设备状态监听
- sqlserver varbinary to base64
- IcoMoon图标字体
- mysql数据库优化
- System.nanoTime()和System.currentTimeMillis()区别 (还是直接看API比较清晰)
- Android Studio或Eclipse找不到逍遥安卓模拟器解决办法
- 音乐| 冥想音乐推荐
- 非空判定工具类
- 2016届360公司PHP服务端开发笔试和面试之所得所感
- android mount --bind 挂载目录
- Hibernate 注解
- javax.net.ssl.SSLHandshakeException(Cas导入证书)
- Spinner实现简单级联