当前位置:网站首页>获取时间戳的三种方法的效率比较
获取时间戳的三种方法的效率比较
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();
}
}
}
边栏推荐
- 学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
- KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
- Analysis of echo service model in the first six chapters of unp
- 在abaqus中使用PyQt设计GUI
- flashfxp 530 User cannot log in. ftp
- El input limit can only input the specified number
- 遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
- Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
- QT designer for QT learning
- 大地坐标系转换火星坐标系
猜你喜欢

有趣的 Kotlin 0x07:Composition

Ansa secondary development - two methods of drawing the middle surface

USB产品(FX3、CCG3PA)的调试方法

Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code

The video Number finds the golden key, and Tiktok imitates the latecomers

Wake up after being repeatedly upset by MQ! Hate code out this MQ manual to help the journey of autumn recruitment

Several methods of HyperMesh running script files

Leetcode learn complex questions with random pointer linked lists (detailed explanation)

有趣的 Kotlin 0x0A:Fun with composition

栈的介绍与实现(详解)
随机推荐
WSL+Valgrind+Clion
快速掌握 Kotlin 集合函数
Signal shielding and processing
nowcode-学会删除链表中重复元素两题(详解)
LeetCode每日一练 —— 160. 相交链表
The local area network cannot access the Apache server
Curl returns blank or null without output. Solve the problem
I can only sell the company after the capital has been "cut off" for two years
Analysis of echo service model in the first six chapters of unp
有趣的 Kotlin 0x0A:Fun with composition
Nowcode- learn to delete duplicate elements in the linked list (detailed explanation)
大地坐标系转换火星坐标系
LwIP development | socket | TCP | client
Detailed record of steps to configure web server (many references)
Introduction and implementation of queue (detailed explanation)
排序2-冒泡排序与快速排序(递归加非递归讲解)
关于MIT6.828_HW9_barriers xv6 homework9的一些问题
Configure HyperMesh secondary development environment on vs Code
ANSYS secondary development - MFC interface calls ADPL file
Rosen's QT journey 101 models and views in QT quick