I have a BFS algorithm to solve the 8-Puzzle, and one of the project requirements is to output the amount of time it takes to find the shallowest solution.
I am using System.nanoTime()
to keep track of the applications run time because it solves the majority of the given puzzles in well under a second.
The problem i am having is whem i convert my nanoTime
to seconds, it displays in a weird format.
the following code is used:
final long startTime = System.nanoTime();
//algorithm code removed for simplicity this all functions correctly
final long duration = System.nanoTime() - startTime;
final double seconds = ((double)duration / 1000000000);
System.out.println("Nano time total: " + duration);
System.out.println("solution Time : " + seconds + " Seconds");
This produces the output:
Nano time total: 916110
solution time : 9.1611E-4 Seconds
I have also tried using floats to represent the values.
is anybody able to provide a better way to convert/display, maybe use a format output statement?
Thanks for taking the time to read my question.
We can just divide the nanoTime by 1_000_000_000 , or use the TimeUnit. SECONDS.
nanoTime() is a great function, but one thing it's not: accurate to the nanosecond. The accuracy of your measurement varies widely depending on your operation system, on your hardware and on your Java version. As a rule of thumb, you can expect microsecond resolution (and a lot better on some systems).
nanoTime() method returns the current value of the most precise available system timer, in nanoseconds. The value returned represents nanoseconds since some fixed but arbitrary time (in the future, so values may be negative) and provides nanosecond precision, but not necessarily nanosecond accuracy.
No, there is no guarantee that every call to System. nanoTime() will return a unique value.
I think you need: DecimalFormat
System.out.println("solution Time : " + new DecimalFormat("#.##########").format(seconds) + " Seconds");
System.out.format("solution Time : %f Seconds", seconds);
for the classic, non-exponential floating point number.
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