[英]What is faster? System.currentTimeMillis() or Date().getTime()?
What is faster method? 什么是更快的方法?
System.currentTimeMillis()
or 要么
new Date().getTime()?
Is there the faster solution to know elapsed time? 知道经过的时间有更快的解决方案吗?
If you do 如果你这样做
new Date()
it calls 它叫
/**
* Allocates a <code>Date</code> object and initializes it so that
* it represents the time at which it was allocated, measured to the
* nearest millisecond.
*
* @see java.lang.System#currentTimeMillis()
*/
public Date() {
this(System.currentTimeMillis());
}
so it calls System.currentTimeMillis() AND creates an object you immediately throw away. 所以它调用System.currentTimeMillis()并创建一个立即丢弃的对象。
If you are very lucky, escape analysis will remove the redundant object and the performance will be much the same. 如果你很幸运,逃逸分析将删除冗余对象,性能将大致相同。
However, I wouldn't assume Escape Analysis will kick in and just call 但是,我不认为Escape Analysis会启动并且只是打电话
long start = System.currentTimeMillis();
// do something
long time = System.currentTimeMillis() - start;
Notes: 笔记:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.