当前位置:网站首页>Efficiency comparison of three methods for obtaining timestamp
Efficiency comparison of three methods for obtaining timestamp
2022-07-28 16:51:00 【Ride a fish ~ cross the sea】
1.System.currentTimeMillis()
System Class currentTimeMillis() The method is the most efficient of the three , The shortest running time . In the development, if the design comes to the problem of efficiency , It is recommended to use this method to obtain .
2.new Date().getTime()
except System class , The ones that use a lot should be Date The class , but date Getting a timestamp in a class is not the most efficient , Look at his source code :
The parameterless structure is as follows
public Date() {
this(System.currentTimeMillis());
}
You can see from the source code ,new Date() It's actually called System.currentTimeMillis(), Then pass in your own parameter constructor .
It's not hard to see. , If you just get the timestamp , Even anonymous new Date() Objects also have a slight performance cost , From the perspective of improving performance , Just get the timestamp , Regardless of the time zone ,
Call directly System.currentTimeMillis() It will be better .
3.Calendar.getInstance().getTimeInMillis()
This method is actually the slowest , Look at its source code and you will find ,Canlendar It's time zone sensitive , Because it takes a lot of time to deal with the time zone problem .
package com.example.java_algorithms.javaBasic.time;
import java.util.Calendar;
import java.util.Date;
/** * Efficiency comparison of three time stamp acquisition methods */
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();
}
}
}
边栏推荐
- egg(十九):使用egg-redis性能优化,缓存数据提升响应效率
- Signal shielding and processing
- Multiple commands produce ‘.../xxx.app/Assets.car‘问题
- ABAQUS GUI interface solves the problem of Chinese garbled code (plug-in Chinese garbled code is also applicable)
- Installation of QT learning
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3 a.ancestor lca+ violence count
- Using pyqt to design gui in ABAQUS
- “蔚来杯“2022牛客暑期多校训练营3 J.Journey 0-1最短路
- Curl returns blank or null without output. Solve the problem
- Cluster construction and use of redis5
猜你喜欢

WSL+Valgrind+Clion

小程序:scroll-view默认滑倒最下面

Optimization of network request success rate in IM instant messaging software development

Ansa secondary development - Introduction to interface development tools

有趣的 Kotlin 0x06:List minus list

IM即时通讯软件开发网络请求成功率的优化

Several methods of HyperMesh running script files

有趣的 Kotlin 0x0A:Fun with composition

Sort 4-heap sort and massive TOPK problem

Sort 1-insert sort and Hill sort
随机推荐
Interesting kotlin 0x08:what am I
Nowcode- learn to delete duplicate elements in the linked list (detailed explanation)
HM secondary development - data names and its use
Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code
局域网无法访问apache服务器
Brother Ali teaches you how to correctly understand the problem of standard IO buffer
“蔚来杯“2022牛客暑期多校训练营3 A.Ancestor LCA+暴力计数
Leetcode daily practice - 160. Cross linked list
Microsoft question 100 - do it every day - question 16
LwIP develops | socket | TCP | keepalive heartbeat mechanism
Configure HyperMesh secondary development environment on vs Code
Ansa secondary development - apps and ansa plug-in management
Signal shielding and processing
Text filtering skills
配置web服务器步骤详细记录(多有借鉴)
Ansa secondary development - build ansa secondary development environment on Visual Studio code
Early in the morning, pay Bora SMS to say that you won the "prize"? Dealing with server mining virus - kthreaddi
智慧园区是未来发展的趋势吗?
Detailed record of steps to configure web server (many references)
HM二次开发 - Data Names及其使用