4
もっと速い方法は何ですか?より速いのは何ですか? System.currentTimeMillis()またはDate()。getTime()?
System.currentTimeMillis()
または
new Date().getTime()?
は、経過時間を知るためのより高速な解決策はありますか?
もっと速い方法は何ですか?より速いのは何ですか? System.currentTimeMillis()または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()を呼び出して、あなたはすぐに捨てるオブジェクトを作成します。
あなたが非常に幸運であれば、エスケープ解析によって冗長オブジェクトが削除され、パフォーマンスはほぼ同じになります。
long start = System.currentTimeMillis();
// do something
long time = System.currentTimeMillis() - start;
ノートを
しかし、私は中にキックされますエスケープ解析を前提とせず、ただ呼び出す:
もっと速いとはどういう意味ですか?より少ないCPUサイクルのように?なぜそれは重要ですか?しかし、 'System.nanoTime()'を使ってより正確なメソッドがありますが、2回取得して_elapsed_時間を自分で計算する必要があります。 – Thomas
[Javaで経過した時間はどのように測定するのですか?](http://stackoverflow.com/questions/1770010/how-do-i-measure-time-elapsed-in-java)を参照してください。 –
私は 'long now = System.currentTimeMillis();を使用しています。 long elapsedTime = now - prevTime; ' – Igor