Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQLite cursor.getCount在Android中的操作是否昂贵_Android_Sqlite - Fatal编程技术网

SQLite cursor.getCount在Android中的操作是否昂贵

SQLite cursor.getCount在Android中的操作是否昂贵,android,sqlite,Android,Sqlite,SQLite cursor.getCount()在Android设备上执行的操作是否昂贵 哪个更快: 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())

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

哪个更快:

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);
int length=cursor.getCount();
最终列表项=新的ArrayList(长度*2);//每行可能需要2件物品
if(cursor.moveToFirst()){
//循环查询结果
做{
...

Cursor Cursor=db.rawQuery(sql,null);
final List items=new ArrayList();//android上的容量默认为0
if(cursor.moveToFirst()){
//循环查询结果
做{

如果比较两个代码示例执行所需的时间,您会注意到这是相同的,因为最终调用的是
SQLiteCursor.getCount()

我不认为性能有显著差异,除非你说的是相当数量/大小的元素。否则,我认为你的问题提供了一些可以讨论的主题:是否最好提前在手机上分配内存以获得性能…我认为性能没有任何差异。
cursor。moveToFirst()
调用
onMove
调用
getCount
。因此getCount可能非常慢,但您无法真正摆脱它。这是正确的,我在一个大型数据库上测试了两个代码示例,查询需要30秒才能完成,时间相同。我想,Ionut Negru已经回答了您的问题:)
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 {