Is SQLite cursor.getCount() expensive operation when executed on Android device?
Which is faster:
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 {
...
or
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 {
If you compare the time required for both code samples to execute, you'll notice it's the same because Cursor.moveToFirst()
is eventually calling SQLiteCursor.getCount()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With