![](/img/trans.png)
[英]Difference between System.currentTimeMillis() and Date getTime()?
[英]What is faster? System.currentTimeMillis() or Date().getTime()?
什么是更快的方法?
System.currentTimeMillis()
要么
new Date().getTime()?
知道经过的时间有更快的解决方案吗?
如果你这样做
new Date()
它叫
/**
* 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());
}
所以它调用System.currentTimeMillis()并创建一个立即丢弃的对象。
如果你很幸运,逃逸分析将删除冗余对象,性能将大致相同。
但是,我不认为Escape Analysis会启动并且只是打电话
long start = System.currentTimeMillis();
// do something
long time = System.currentTimeMillis() - start;
笔记:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.