2013-11-02 16 views
4

我有一個BFS算法來解決8-Puzzle,並且其中一個項目要求是輸出找到最淺解決方案所花費的時間。如何正確顯示nanotime到第二次轉換

我使用System.nanoTime()來跟蹤應用程序的運行時間,因爲它能夠在一秒鐘內解決大部分給定的難題。

我遇到的問題是我是否將我的nanoTime轉換爲秒,它以奇怪的格式顯示。

以下代碼用於:

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"); 

這將產生輸出:

Nano time total: 916110 
solution time : 9.1611E-4 Seconds 

我已經使用浮標表示值也嘗試。

是誰能夠提供更好的方式來轉換/顯示,也許使用格式輸出語句?

感謝您花時間閱讀我的問題。

+4

9.1611E-4是表示數量的有效途徑。這只是科學記數法。 – hexafraction

+0

關於格式化雙打有很多問題。你可以去看看他們。 – Justin

回答

7

我想你需要:DecimalFormat的

System.out.println("solution Time : " + new DecimalFormat("#.##########").format(seconds) + " Seconds"); 
+0

謝謝,我不知道有這樣的方法。我現在要花一點時間來看API。謝謝大家。 –

5
System.out.format("solution Time : %f Seconds", seconds); 

爲經典的,非指數浮點數。

相關問題