简体   繁体   English

Android Wear:有没有理由使用Time对象而不是Calendar对象?

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

Here's what I know, if there's any errors, let me know. 这就是我所知道的,如果有任何错误,请告诉我。

Example watch faces, like the analog watch face , in the SDK use a deprecated Time object for managing time. 示例监视面(如模拟监视面 )在SDK中使用不推荐使用的Time对象来管理时间。

According to the documentation Time was deprecated in level 22 (Android 5.1). 根据文档时间在22级(Android 5.1)中被弃用。 Now obviously it still has a lot of life, but in the interests of future proofing code I looked I looked at switching to the Calendar object. 现在显然它仍然有很多生命,但为了未来验证代码的利益,我看着我看着切换到Calendar对象。

I believe both Time and Calendar are fancy wrappers for a long variable. 我相信时间和日历都是长期变量的花哨包装。 I wrote this benchmark to test their speed. 我写了这个基准来测试他们的速度。

            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;
            }

The benchmark results show Calendar as 2x faster running it on a Moto 360. 基准测试结果显示,在Moto 360上运行日历的速度提高了2倍。

Switching a test watch face to Calendar shows no memory being leaked in the debugger. 将测试表面切换到日历表示调试器中没有泄漏内存。

So my question is two fold. 所以我的问题是双重的。 Is there a problem with my benchmark, or is it indeed faster? 我的基准测试有问题,还是确实更快? If so, what is the advantage of Time, such that they used it in their examples? 如果是这样,Time的优点是什么,以便他们在他们的例子中使用它?

My hypothesis is that they just used it to make their examples more understandable. 我的假设是他们只是用它来使他们的例子更容易理解。 Time is a more intuitive name, but I'd like to know if there's a technical reason. 时间是一个更直观的名称,但我想知道是否有技术原因。

When the samples were written, the Time class wasn't deprecated yet. 编写样本时, Time类尚未弃用。 We would use Calendar if we wrote them now. 如果我们现在写它们,我们会使用Calendar Use Calendar . 使用Calendar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM