[Android] OrmLite performance test: Insert vs Bulk-insert

1.- Parse a 800 Kb JSON file into an array
2.- Insert all the parsed entities into a table (Using OrmLite)
3.- Empty the table
4.- Perform the same insertion, but using Bulk-insert (one single transaction for all inserts, instead of a transaction for each)

Tested code:


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orm_lite_main);
strb = new StringBuilder();
mTxt1 = (TextView) findViewById(R.id.txt1);

long startMs = System.currentTimeMillis();
ArrayList points = parsePointsJson_v1(this);
long endMs = System.currentTimeMillis();

strb.append(points != null ? points.size() : "")
.append(points != null ? " points parsed" : " parse error")
.append(" - ").append(endMs-startMs).append(" milliseconds\n");

int insertCount=0;
try{
final Dao poiDao = getHelper().getPointOfInterestDao();
poiDao.deleteBuilder().delete();

startMs = System.currentTimeMillis();
for(PointOfInterest p : points){
poiDao.create(p);
++insertCount;
}
endMs = System.currentTimeMillis();
strb.append(insertCount)
.append(" normal insertions - ")
.append(endMs-startMs).append(" milliseconds\n\n");

startMs = System.currentTimeMillis();
poiDao.deleteBuilder().delete();
endMs = System.currentTimeMillis();
strb.append("Empty Points table took: ").append(endMs-startMs).append(" milliseconds\n");

final ArrayList finalPoints = points;
TransactionManager.callInTransaction(poiDao.getConnectionSource(), new Callable(){
public Void call() throws Exception {
long start = System.currentTimeMillis();
int insertCount2 = 0;
for(PointOfInterest fp : finalPoints ){
poiDao.create(fp);
++insertCount2;
}
long end = System.currentTimeMillis();
strb.append(" Bulk insert: ").append(insertCount2).append(" insertions. Took: ").append(end-start).append(" milliseconds\n");
return null;
}
});

}catch(SQLException e){
strb.append(" an exception ocurred while inserting: ").append( e.getMessage()).append(" ").append(insertCount).append(" insertions were made");
e.printStackTrace();
}catch(Exception e){
strb.append(" Exception bulk-inserting: "+ e.getMessage());
e.printStackTrace();
}

mTxt1.setText(strb.toString());
}

Results on some real devices:

HTC Sensation z710e
htc1

Samsung Galaxy SII
sgs1
BQ Aquaris 5
Screenshot_2014-11-12-16-37-55

A little compilation of the results:
results

Advertisements

One thought on “[Android] OrmLite performance test: Insert vs Bulk-insert

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s