簡體   English   中英

Android Wear:有沒有理由使用Time對象而不是Calendar對象?

[英]Android Wear: Is there any reason to use a Time object rather than a Calendar object?

這就是我所知道的,如果有任何錯誤,請告訴我。

示例監視面(如模擬監視面 )在SDK中使用不推薦使用的Time對象來管理時間。

根據文檔時間在22級(Android 5.1)中被棄用。 現在顯然它仍然有很多生命,但為了未來驗證代碼的利益,我看着我看着切換到Calendar對象。

我相信時間和日歷都是長期變量的花哨包裝。 我寫了這個基准來測試他們的速度。

            long timeStart = 0;
            long timeEndcalendarStart = 0;
            long timeDifference = 0;
            long calendarEnd = 0;
            long calendarDifference = 0;


            for (int index = 0; index < 30000; index++) {

                timeStart = System.currentTimeMillis();
                Time testTime = new Time();
                testTime.setToNow();
                long mills = testTime.toMillis(false);
                float seconds = testTime.second;
                float minutes = testTime.minute;
                float hours = testTime.hour;

                timeEndcalendarStart = System.currentTimeMillis();

                Calendar testCalendar = Calendar.getInstance();
                long cmills = testCalendar.getTimeInMillis();
                float cseconds = testCalendar.get(Calendar.SECOND);
                float cminutes = testCalendar.get(Calendar.MINUTE);
                float chours = testCalendar.get(Calendar.HOUR);
                calendarEnd = System.currentTimeMillis();

                timeDifference += timeEndcalendarStart - timeStart;
                calendarDifference += calendarEnd - timeEndcalendarStart;
            }

基准測試結果顯示,在Moto 360上運行日歷的速度提高了2倍。

將測試表面切換到日歷表示調試器中沒有泄漏內存。

所以我的問題是雙重的。 我的基准測試有問題,還是確實更快? 如果是這樣,Time的優點是什么,以便他們在他們的例子中使用它?

我的假設是他們只是用它來使他們的例子更容易理解。 時間是一個更直觀的名稱,但我想知道是否有技術原因。

編寫樣本時, Time類尚未棄用。 如果我們現在寫它們,我們會使用Calendar 使用Calendar

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM