What is faster method?
System.currentTimeMillis()
or
new Date().getTime()?
Is there the faster solution to know elapsed time?
System. currentTimeMillis() takes about 29 nanoseconds per call while System.
currentTimeMillis() . This method reports current time with the millisecond accuracy. One may think that, because of this, the performance of this method is irrelevant.
currentTimeMillis() method returns the current time in milliseconds. The unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.
json"(February 26, 2019 12:00:00 AM) and that need to be accessed from android app once device's System. currentTimeMillis() returns exact 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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With