How to convert System.currentTimeMillis();
to seconds?
long start6=System.currentTimeMillis();
System.out.println(counter.countPrimes(100000000)+" for "+start6);
The console shows me 5761455 for 1307816001290
.
I can't read how many seconds that is.
Any help?
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.
System. currentTimeMillis() returns the current time in milliseconds. The method returns time difference in milliseconds between the current time and midnight, January 1, 1970 (UTC or epoch time).
System. currentTimeMillis() returns a Java long . It will always be exactly 64 bits long and will typically have a number of leading zeroes.
This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Depending on the system, it can take more than 100 cpu cycles to execute.
TimeUnit
Use the TimeUnit
enum built into Java 5 and later.
long timeMillis = System.currentTimeMillis(); long timeSeconds = TimeUnit.MILLISECONDS.toSeconds(timeMillis);
long start = System.currentTimeMillis();
counter.countPrimes(1000000);
long end = System.currentTimeMillis();
System.out.println("Took : " + ((end - start) / 1000));
UPDATE
An even more accurate solution would be:
final long start = System.nanoTime();
counter.countPrimes(1000000);
final long end = System.nanoTime();
System.out.println("Took: " + ((end - start) / 1000000) + "ms");
System.out.println("Took: " + (end - start)/ 1000000000 + " seconds");
like so:
(int)(milliseconds / 1000)
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