Android Wear:有没有理由使用时间对象而不是日历对象?
Android Wear: Is there any reason to use a Time object rather than a Calendar object?
这是我所知道的,如果有任何错误,请告诉我。
示例表盘,如 analog watch face,在 SDK 中使用已弃用的时间对象来管理时间。
根据文档,Time 在级别 22 (Android 5.1) 中已弃用。现在显然它还有很多生命,但为了将来的校对代码,我查看了切换到 Calendar 对象。
我相信 Time 和 Calendar 都是 long 变量的奇特包装器。我写了这个基准来测试他们的速度。
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
class 尚未弃用。如果我们现在写它们,我们会使用 Calendar
。使用 Calendar
.
这是我所知道的,如果有任何错误,请告诉我。
示例表盘,如 analog watch face,在 SDK 中使用已弃用的时间对象来管理时间。
根据文档,Time 在级别 22 (Android 5.1) 中已弃用。现在显然它还有很多生命,但为了将来的校对代码,我查看了切换到 Calendar 对象。
我相信 Time 和 Calendar 都是 long 变量的奇特包装器。我写了这个基准来测试他们的速度。
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
class 尚未弃用。如果我们现在写它们,我们会使用 Calendar
。使用 Calendar
.