当前位置:网站首页>System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
2022-07-04 10:08:00 【Java technology stack】
author : A fabricated belief
source :https://segmentfault.com/a/1190000041276485
Java There are two ways to get a timestamp :System.currentTimeMillis()
and System.nanoTime()
, Their usage scenarios are different , At present, some articles on the Internet have some one-sided descriptions about the performance of these two methods , This article hopes to give a simple final answer .
System.currentTimeMillis() There are performance issues ?
The answer is No . The performance difference between the two methods depends on the operating system .
Windows:
stay Windows Next ,System.currentTimeMillis()
Than System.nanoTime()
Much faster , This is because Windows The system only provides a cache variable for the former , The latter is to get the count from the bottom of the hardware in real time .
So if your production environment is Windows, Please avoid using
System.nanoTime()
.
Linux:
stay Linux Next , There is little difference in the execution time between the two , Whether it's single threaded or multi-threaded .
Different virtual machine implementations can lead to performance differences
Today's cloud hosts mainly include Xen and KVM Two ways of implementation , Some articles on the Internet found that they have performance differences in taking system time .
When your virtual machine uses Xen when , Taking time will be KVM More than ten times . However, the above also provides solutions to such problems .
You need to write a special class to improve System.currentTimeMillis() Performance ?
Unwanted . That's painting a snake to add feet .
My test code
My test code is as follows , Without any dependency , It can be used directly javac Compile and run . Readers who are interested can try :
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class TimePerformance {
public static final int LOOP_COUNT = 9999999;
public static final int THREAD_COUNT = 30;
public static void main(String[] args) {
Runnable millisTest = () -> {
long start = System.currentTimeMillis();
for (int i = 0; i < LOOP_COUNT; i++) {
System.currentTimeMillis();
}
long end = System.currentTimeMillis();
System.out.printf("%s : %f ns per call\n",
Thread.currentThread().getName(), ((double)end - start) * 1000000 / LOOP_COUNT);
};
Runnable nanoTest = () -> {
long start = System.currentTimeMillis();
for (int i = 0; i < LOOP_COUNT; i++) {
System.nanoTime();
}
long end = System.currentTimeMillis();
System.out.printf("%s : %f ns per call\n",
Thread.currentThread().getName(), ((double)end - start) * 1000000 / LOOP_COUNT);
};
Consumer<Runnable> testing = test -> {
System.out.println("Single thread test:");
test.run();
System.out.println(THREAD_COUNT + " threads test:");
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < THREAD_COUNT; i++) {
Thread t = new Thread(test);
t.start();
threads.add(t);
}
// Wait for all threads to finish
threads.forEach(thread -> {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
};
System.out.println(" Test System.nanoTime()");
testing.accept(nanoTest);
System.out.println(" Test System.currentTimeMillis()");
testing.accept(millisTest);
}
}
Because I'm using Windows, So in the execution output System.nanoTime()
Obviously very slow .
I won't release the specific output content , Because it has no reference value , Most production environments use Linux.
Recent hot article recommends :
1.1,000+ Avenue Java Arrangement of interview questions and answers (2022 The latest version )
2. Explode !Java Xie Cheng is coming ...
3.Spring Boot 2.x course , It's too complete !
4. Don't write about the explosion on the screen , Try decorator mode , This is the elegant way !!
5.《Java Development Manual ( Song Mountain version )》 The latest release , Download it quickly !
I think it's good , Don't forget to like it + Forward !
边栏推荐
- Exercise 7-3 store the numbers in the array in reverse order (20 points)
- Summary of small program performance optimization practice
- Write a jison parser from scratch (4/10): detailed explanation of the syntax format of the jison parser generator
- Go context 基本介绍
- C # use gdi+ to add text to the picture and make the text adaptive to the rectangular area
- Write a jison parser (7/10) from scratch: the iterative development process of the parser generator 'parser generator'
- 入职中国平安三周年的一些总结
- Baidu R & D suffered Waterloo on three sides: I was stunned by the interviewer's set of combination punches on the spot
- 智能网关助力提高工业数据采集和利用
- Intelligent gateway helps improve industrial data acquisition and utilization
猜你喜欢
pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.
Regular expression (I)
用数据告诉你高考最难的省份是哪里!
Histogram equalization
Normal vector point cloud rotation
C语言指针面试题——第二弹
leetcode1-3
Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
Hands on deep learning (38) -- realize RNN from scratch
Hands on deep learning (46) -- attention mechanism
随机推荐
Exercise 7-3 store the numbers in the array in reverse order (20 points)
View CSDN personal resource download details
Exercise 8-7 string sorting (20 points)
Whether a person is reliable or not, closed loop is very important
查看CSDN个人资源下载明细
Press the button wizard to learn how to fight monsters - identify the map, run the map, enter the gang and identify NPC
Hands on deep learning (41) -- Deep recurrent neural network (deep RNN)
Hands on deep learning (42) -- bi-directional recurrent neural network (BI RNN)
有老师知道 继承RichSourceFunction自定义读mysql怎么做增量吗?
Machine learning -- neural network (IV): BP neural network
ASP. Net to access directory files outside the project website
libmysqlclient.so.20: cannot open shared object file: No such file or directory
2021-08-11 function pointer
Does any teacher know how to inherit richsourcefunction custom reading Mysql to do increment?
【Day2】 convolutional-neural-networks
xxl-job惊艳的设计,怎能叫人不爱
原生div具有编辑能力
今日睡眠质量记录78分
C语言指针面试题——第二弹
[200 opencv routines] 218 Multi line italic text watermark