【发布时间】:2023-03-24 03:04:01
【问题描述】:
嗯,Performance Tips 中说:
因此,您应该默认使用增强的 for 循环,但请考虑使用 用于性能关键型 ArrayList 的手写计数循环 迭代。
但是查看ioshed 2013 应用程序,它被认为是大多数开发人员的示例,特别是在ScheduleUpdaterService.java 中,我可以看到以下内容:
void processPendingScheduleUpdates() {
try {
// Operate on a local copy of the schedule update list so as not to block
// the main thread adding to this list
List<Intent> scheduleUpdates = new ArrayList<Intent>();
synchronized (mScheduleUpdates) {
scheduleUpdates.addAll(mScheduleUpdates);
mScheduleUpdates.clear();
}
SyncHelper syncHelper = new SyncHelper(this);
for (Intent updateIntent : scheduleUpdates) {
String sessionId = updateIntent.getStringExtra(EXTRA_SESSION_ID);
boolean inSchedule = updateIntent.getBooleanExtra(EXTRA_IN_SCHEDULE, false);
LOGI(TAG, "addOrRemoveSessionFromSchedule:"
+ " sessionId=" + sessionId
+ " inSchedule=" + inSchedule);
syncHelper.addOrRemoveSessionFromSchedule(this, sessionId, inSchedule);
}
} catch (IOException e) {
// TODO: do something useful here, like revert the changes locally in the
// content provider to maintain client/server sync
LOGE(TAG, "Error processing schedule update", e);
}
}
请注意通过scheduleUpdates 有一个增强的for 循环迭代,同时建议避免ArrayList 的这种类型的迭代。
这是因为从性能的角度来看,应用程序的这一部分不被认为是关键的,还是我不理解某些东西?非常感谢。
【问题讨论】:
标签: java android performance arraylist