![](/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.