当前位置:网站首页>获取时间戳的三种方法的效率比较
获取时间戳的三种方法的效率比较
2022-07-28 15:45:00 【骑鱼~过海】
1.System.currentTimeMillis()
System类中的currentTimeMillis()方法是三种方式中效率最好的,运行时间最短。开发中如果设计到效率问题,推荐使用此种方式获取。
2.new Date().getTime()
除了System类,使用量很大的应该就是Date类了,但date类中获取时间戳并不是最有效率的,翻看他的源码:
无参构造如下
public Date() {
this(System.currentTimeMillis());
}
从源码可以看出,new Date()其实就是调用了System.currentTimeMillis(),再传入自己的有参构造函数。
不难看出,如果只是仅仅获取时间戳,即使是匿名的new Date()对象也会有些许的性能消耗,从提升性能的角度来看,只是仅仅获取时间戳,不考虑时区的影响,
直接调用System.currentTimeMillis()会更好一些。
3.Calendar.getInstance().getTimeInMillis()
这种方式其实是速度最慢,看其源码就会发现,Canlendar是区分时区的,因为要处理时区问题会耗费很多的时间。
package com.example.java_algorithms.javaBasic.time;
import java.util.Calendar;
import java.util.Date;
/** * 三种时间戳获取方式效率比较 */
public class TimeStampComparable {
private static long _TEN_THOUSAND=10000;
public static void main(String[] args) {
long times=1000*_TEN_THOUSAND;
long t1=System.currentTimeMillis();
testSystem(times);
long t2=System.currentTimeMillis();
System.out.println(t2-t1);
testCalander(times);
long t3=System.currentTimeMillis();
System.out.println(t3-t2);
testDate(times);
long t4=System.currentTimeMillis();
System.out.println(t4-t3);
/** testSystem:28 testCalander:1513 testDate:29 */
}
public static void testSystem(long times){
//use 188
for(int i=0;i<times;i++){
long currentTime=System.currentTimeMillis();
}
}
public static void testCalander(long times){
//use 6299
for(int i=0;i<times;i++){
long currentTime= Calendar.getInstance().getTimeInMillis();
}
}
public static void testDate(long times){
for(int i=0;i<times;i++){
long currentTime=new Date().getTime();
}
}
}
边栏推荐
- Ansa secondary development - Introduction to interface development tools
- 排序4-堆排序与海量TopK问题
- Using pyqt to design gui in ABAQUS
- Leetcode daily practice - the number of digits in the offer 56 array of the sword finger
- 微信公众号获取素材列表
- CRC16数据校验支持ModelBus和XMODEM校验模式(C语言)
- 有趣的 Kotlin 0x07:Composition
- 1. Simple command line connection to database
- 2021-04-02
- Ansa secondary development - two methods of drawing the middle surface
猜你喜欢

8051 series MCU firmware upgrade IAP

Im im development optimization improves connection success rate, speed, etc

LeetCode-学会复杂带随机指针链表的题(详解)

WSL+Valgrind+Clion

一大早支付宝来短信说你中“奖”了?处理服务器挖矿病毒 - kthreaddi

The epidemic dividend disappeared, and the "home fitness" foam dissipated

Use js direct OSS to store files in Alibaba cloud and solve the limitation of large file upload server

HM secondary development - data names and its use

栈的介绍与实现(详解)

Ansa secondary development - Introduction to interface development tools
随机推荐
Curl returns blank or null without output. Solve the problem
redis源码优化--绑核
QT packaging
WSL+Valgrind+Clion
LeetCode每日一练 —— 160. 相交链表
Fx3 development board and schematic diagram
队列的介绍与实现(详解)
信号屏蔽与处理
大地坐标系转换火星坐标系
Qt学习之信号和槽机制
有趣的 Kotlin 0x09:Extensions are resolved statically
PHP 图片上传
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 acfhj
微信公众号获取素材列表
遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
Pop up layer prompt in the background
ANSA二次开发 - Apps和ANSA插件管理
排序1-插入排序与希尔排序
About the web docking pin printer, lodop uses
Im im development optimization improves connection success rate, speed, etc