关于Android中SharedPreferences提交数据效率的调研
在浏览器客户端数据初始化数据优化时过程中,由于多次看到使用SharedPreferences保存数据。于是查了下客户端的SharedPreferencesManager的源码,发现我们提交数据时的代码形式如下:
1. public void putFloat(String key, float value)
2. {
3. editor.putFloat(key, value);
4. editor.commit();
5. }
即我们每次都使用事务提交数据,这样操作对客户端来说是很安全的,能确保每次数据能够及时写入,但是,由此也带来了一个小问题,那就是commit操作本身耗时时间时比较长的,多次commit必然会带来时间和性能上的比较大的开销。客户端做了一个Demo来调研该设想,代码如下:
1.每次使用commit提交数据,循环50次,每次提交3条数据
1. button.setOnClickListener(new OnClickListener() {
2.
3. @Override
4. public void onClick(View v) {
5. SharedPreferenceManager manager = SharedPreferenceManager.getInstance();
6. Long startTime = Calendar.getInstance().getTimeInMillis();
7. Log.e("start","~"+startTime );
8. for(int i=0;i<50;i++){
9. manager.putIntCom("first"+i, 1);
10. manager.putIntCom("second"+i, 2);
11. manager.putIntCom("third"+i, 3);
12. }
13. // manager.commit();
14. Long endTime = Calendar.getInstance().getTimeInMillis();
15. Log.e("endTime","~"+endTime );
16. Log.e("time---->", ""+(endTime-startTime));
17. Log.e("average", ""+(endTime-startTime)/50);
18. }
19. });
manager.putIntCom的方法如下:
1. public void putIntCom(String key, int value){
2. editor.putInt(key, value);
3. editor.commit();
4. }
来看日志:
1. 04-05 03:38:54.224: E/start(9713): ~1333597134230
2. 04-05 03:38:55.024: D/dalvikvm(9713): GC_FOR_MALLOC freed 2463 objects / 413552 bytes in 107ms
3. 04-05 03:38:55.774: D/dalvikvm(9713): GC_FOR_MALLOC freed 1698 objects / 566968 bytes in 89ms
4. 04-05 03:38:56.724: D/dalvikvm(9713): GC_FOR_MALLOC freed 1678 objects / 507304 bytes in 48ms
5. 04-05 03:38:58.254: D/dalvikvm(9713): GC_FOR_MALLOC freed 1600 objects / 529256 bytes in 45ms
6. 04-05 03:39:00.273: D/dalvikvm(9713): GC_FOR_MALLOC freed 1623 objects / 505208 bytes in 44ms
7. 04-05 03:39:01.264: D/dalvikvm(9713): GC_FOR_MALLOC freed 1600 objects / 529216 bytes in 46ms
8. 04-05 03:39:01.554: D/dalvikvm(9713): GC_FOR_MALLOC freed 1623 objects / 505152 bytes in 47ms
9. 04-05 03:39:01.874: D/dalvikvm(9713): GC_FOR_MALLOC freed 1600 objects / 529216 bytes in 45ms
10. 04-05 03:39:02.094: E/endTime(9713): ~1333597142101
11. 04-05 03:39:02.094: E/time---->(9713): 7871
12. 04-05 03:39:02.094: E/average(9713): 157
日志的最后一条显示:平均提交耗时约为157毫秒,当然这里每次提交3条记录,那么每次commit大概耗时52毫秒
2.采用先提交,最后一次性commit方法 www.zzzyk.com
1. button.setOnClickListener(new OnClickListener() {
2.
3. @Override
4. public void onClick(View v) {
5. SharedPreferenceManager manager = SharedPreferenceManager.getInstance();
6. Long startTime = Calendar.getInstance().getTimeInMillis();
7. Log.e("start","~"+startTime );
8. for(int i=0;i<50;i++){
9. manager.putInt("first"+i, 1);
10. manager.putInt("second"+i, 2);
11. manager.putInt("third"+i, 3);
12. }
13. manager.commit();
14. Long endTime = Calendar.getInstance().getTimeInMillis();
15. Log.e("endTime","~"+endTime );
16. Log.e("time---->", ""+(endTime-startTime));
17. Log.e("average", ""+(endTime-startTime)/50);
18. }
19. });
Manager.putInt()方法形如:
1. public void putInt(String key, int value)
2. {
3. editor.putInt(key, value);
4. }
日志如下:
1. 04-05 03:36:46.214: E/start(9167): ~1333597006214
2. 04-05 03:36:46.244: E/endTime(9167): ~1333597006253
3. 04-05 03:36:46.244: E/time---->
补充:移动开发 , Android ,