手记

Android 性能优化--数据库优化其他优化(五)

(1)语句的拼接使用StringBuilder代替String

简单的String相加会导致创建多个临时对象消耗性能。StringBuilder的空间预分配性能好得多。

如果你对字符串的长度大致了解,如100字符左右,可以直接new StringBuilder(128)指定初始大小,减少空间不够的再次分配。

(2)读写表

在写表时调用sqliteOpenHelper.getWritableDatabase(),在读表时候调用spliteOpenHelper.getReadableDatabase()性能更优。

(3)查询时返回更少的结果集及更少的字段。

查询时只取需要的字段和结果集,更多的结果集会消耗更多的时间及内存,更多的字段会导致更多的内存消耗。

(4)少用cursor.getColumnInndex

根据性能调优过程中的观察cursor.getColumnInndex的时间消耗跟cursor.getInt相差无几。可以在建表的时候用Static变量记住某列的index。直接调用相应index而不是每次查询。

public static final String HTTP_RESPONSE_TABLE_ID   =   android.provider.BaseColumns._ID;

public static final String HTTP_RESPONSE_TABLE_RESPONSE  =  "response";

public list<Object> getData(){

...

cursor.getString(cursor.getColumnIndex(HTTP_RESPONSE_TABLE_RESPONSE));

......

}

-------------------------------------------------------------------

优化为:

public static final String  HTTP_RESPONSE_TABLE_ID   =   android.provider.BaseColumns._ID;

public static final String  HTTP_RESPONSE_TABLE_RESPONSE  =  "response";  

public static final String HTTP_RESPONSE_TABLE_ID_INDEX  = 0;

public static final String HTTP_RESPONSE_TABLE_URL_INDEX = 1;

public list<Object>getData(){

......

cursor.getSring(HTTP_RESPONSE_TABLE_URL_INDEX);

......

}

原文链接:http://www.apkbus.com/blog-783674-61169.html

0人推荐
随时随地看视频
慕课网APP