【问题标题】:Is SQLite cursor.getCount expensive operation in AndroidSQLite cursor.getCount 在 Android 中是昂贵的操作吗
【发布时间】:2011-12-23 16:45:52
【问题描述】:

Android 设备上执行 SQLite cursor.getCount() 操作是否昂贵?

哪个更快:

Cursor cursor = db.rawQuery(sql, null);
int length = cursor.getCount();
final List<Item> items = new ArrayList<Item>(length * 2); // need maybe 2 items per row

if (cursor.moveToFirst()) {

      // loop trough the query result
      do {
...

Cursor cursor = db.rawQuery(sql, null);
final List<Item> items = new ArrayList<Item>();  // capacity is 0 by default on android

if (cursor.moveToFirst()) {

      // loop trough the query result
      do {

【问题讨论】:

  • 我认为性能没有显着差异,除非您谈论的是相当数量/大小的元素。否则,我认为您的问题提供了一些可以讨论的主题:提前在手机上分配内存以获得性能是否更好......
  • 我认为性能没有任何差异。 cursor.moveToFirst() 调用 onMove,后者调用 getCount。所以 getCount 可能会很慢,但你无法真正摆脱它。

标签: android sqlite


【解决方案1】:

如果您比较两个代码示例执行所需的时间,您会发现它是相同的,因为 Cursor.moveToFirst() 最终会调用 SQLiteCursor.getCount()

【讨论】:

  • 这是正确的,我在一个大型数据库上测试了两个代码示例,查询需要 30 秒才能完成,时间相同。
  • 我想,Ionut Negru 已经回答了你的问题 :)
猜你喜欢
  • 2010-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 2010-09-23
  • 2020-06-24
相关资源
最近更新 更多