What is faster method?
System.currentTimeMillis()
or
new Date().getTime()?
Is there the faster solution to know elapsed time?
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.
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
long start = System.currentTimeMillis();
// do something
long time = System.currentTimeMillis() - start;
Notes: