mercredi 2 mars 2016

Realm taking more time for read and write then SQLite

I am trying to replace SQLite with Realm in an android application.

Following is pseudo code of how I am trying to do it:

//SQLite Insert
public void deleteExistingAndInsertNewItemsInDb(ArrayList<Item> itemList, int userId) {
    Log.d("REALM_TEST", "No. of Records :: " + itemList.size());
    long start;
    if (itemList != null && itemList.size() > 0) {
        start = System.nanoTime();
        initializeDb();
        db.beginTransaction();
        // delete existing items from the item list table for the provided userId
        db.delete(DataBaseHelper.TABLE_NAME, DataBaseHelper.USER_ID + "=" + userId, null);
        // insert new item list into local DB.
        for(Item item : itemList) {
            ContentValues contentValues = new ContentValues();
            contentValues.put(DataBaseHelper.COL1, item.getProperty1());
            contentValues.put(DataBaseHelper.COL2, item.getProperty2());
            ...
            ...
            contentValues.put(DataBaseHelper.COL50, item.getProperty50());
            // insert item details into local DB.
            db.insert(DataBaseHelper.TABLE_NAME, null, contentValues);
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        // close database.
        closeDb();
        long sum = System.nanoTime() - start;
        Log.d("REALM_TEST", "Sqlite Insert :: " + sum/1000000);
    }
}  


//SQLite Read
public ArrayList<Item> getItemListFromDB(int userId){

    ArrayList<Item> itemList = new ArrayList<>();
    initializeDb();
    long start = System.nanoTime();
    Cursor cursor = db.query(DataBaseHelper.TABLE_NAME, DataBaseHelper.COLUMN_LIST, DataBaseHelper.USER_ID + "=" + userId,
            null, null, null, null);
    if(cursor != null) {
        while(cursor.moveToNext()) {
            Item item= new Item();
            item.setProperty1(cursor.getString(0));
            item.setProperty2(cursor.getString(1));
            ...
            ...
            ...
            item.setProperty50(cursor.getString(50));

            itemList.add(item);
        }
    }
    closeDb();
    sum = System.nanoTime()-start;
    Log.d("REALM_TEST", "Sqlite read list :: " + sum / 1000000 + "ms For " + itemList.size() + " records");
    return itemList;
}

//Realm Insert
public void deleteExistingAndInsertNewItemsInRealm(ArrayList<Item> itemList, int userId) {

    Realm realm = Realm.getInstance(Application.getContext());
    long start = System.nanoTime();
    //delete all objects
    RealmResults<RealmItem> results = realm.where(RealmItem.class).findAll();
    realm.beginTransaction();
    results.clear();

    for (Item item : itemList) {
        RealmItem realmItem = realm.createObject(RealmItem.class);
        realmItem.setProperty1(item.getProperty1());
        realmItem.setProperty2(item.getProperty2());
        ...
        ...
        realmItem.setProperty50(item.getProperty50());
    }
    realm.commitTransaction();
    long sum = System.nanoTime() - start;
    Log.d("REALM_TEST", "Realm Insert :: " + sum/1000000);
}


//Realm Read
public ArrayList<Item> getItemListFromRealm(int userId) {

    ArrayList<Item> itemList = new ArrayList<>();
    Realm realm = Realm.getInstance(Application.getContext());
    long start = System.nanoTime();
    RealmResults<RealmItem> results =  realm.where(RealItem.class)
            .equalTo("USER_ID", userId).findAll();
    if(results != null) {
        for (RealItem result: results)  {

            Item item= new Item();
            item.setProperty1(result.getProperty1());
            item.setProperty2(cursor.getProperty2());
            ...
            ...
            ...
            item.setProperty50(cursor.getProperty50());

            itemList.add(item);
        }
    }
    sum = System.nanoTime()-start;
    Log.d("REALM_TEST", "Realm read list :: " + sum / 1000000 + "ms For " + itemList.size() + " records");
    return itemList;
}

for 500 items following is read write time in milli seconds:

+--------+-------+--------+-------+
|  Read          | Write          |
+--------+-------+--------+-------+
| Sqlite | Realm | Sqlite | Realm |
| 247    | 382   | 142    | 222   |
| 303    | 321   | 102    | 278   |
| 263    | 303   | 134    | 240   |
| 301    | 292   | 165    | 290   |
+--------+-------+--------+-------+

Am i doing something wrong?

Aucun commentaire:

Enregistrer un commentaire