Survey on the efficiency of SharedPreferences data submission in Android

Source: Internet
Author: User

In the process of initializing Data Optimization on the browser client, because SharedPreferences is used to save data for multiple times. So I checked the source code of the client's SharedPreferencesManager and found that the code format for data submission is as follows:

 
1. public void putFloat (String key, float value)
2 .{
3. editor. putFloat (key, value );
4. editor. commit ();
5 .}


That is, we use transactions to submit data every time. This operation is very secure for the client and ensures that data can be written in time. However, this also brings about a small problem, that is, the commit operation itself takes a long time, and multiple commit operations will inevitably lead to a large cost of time and performance. The client made a Demo to investigate the idea. The Code is as follows:

1. Each time you use commit to submit data, 50 cycles, 3 data records are submitted each time
 
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 .});


The manager. putIntCom method is as follows:
 
1. public void putIntCom (String key, int value ){
2. editor. putInt (key, value );
3. editor. commit ();
4 .}

View the log:
 
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 107 ms
3. 04-05 03:38:55. 774: D/dalvikvm (9713): GC_FOR_MALLOC freed 1698 objects/566968 bytes in 89 ms
4. 04-05 03:38:56. 724: D/dalvikvm (9713): GC_FOR_MALLOC freed 1678 objects/507304 bytes in 48 ms
5. 04-05 03:38:58. 254: D/dalvikvm (9713): GC_FOR_MALLOC freed 1600 objects/529256 bytes in 45 ms
6. 04-05 03:39:00. 273: D/dalvikvm (9713): GC_FOR_MALLOC freed 1623 objects/505208 bytes in 44 ms
7. 04-05 03:39:01. 264: D/dalvikvm (9713): GC_FOR_MALLOC freed 1600 objects/529216 bytes in 46 ms
8. 04-05 03:39:01. 554: D/dalvikvm (9713): GC_FOR_MALLOC freed 1623 objects/505152 bytes in 47 ms
9. 04-05 03:39:01. 874: D/dalvikvm (9713): GC_FOR_MALLOC freed 1600 objects/529216 bytes in 45 ms
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


The last line of the log shows that the average submission time is about 157 milliseconds. Of course, if three records are submitted at a time, each commit takes about 52 milliseconds.

2. Use the first commit method and the last one-time commit method www.2cto.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 .});


The Manager. putInt () method is shown as follows:
 
1. public void putInt (String key, int value)
2 .{
3. editor. putInt (key, value );
4 .}


The log is as follows:
 
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 ----> (9167): 39
4. 04-05 03:36:46. 254: E/average (9167): 0


The data shown here is amazing!

Let's check the implementation method of Sharedpreferences.

 
1. private static final class SharedPreferencesImpl implements SharedPreferences


When putXxx is implemented, the internal Map cache is used to store the data in the Map. When commit is used, the Map is traversed and the data is updated through its Listeners listener.

 
1. listener. onSharedPreferenceChanged (SharedPreferencesImpl. this, key );


By default, we have not rewritten the onSharedPreferenceChanged method, which is implemented by Android itself. However, it can be determined that the commit method involves synchronization, traversal, and other operations, which are time-consuming.
Therefore, reducing the number of commit operations is a matter of optimization.


Conclusion:
The Commit operation is time-consuming. To ensure data security, you can change the Commit operation as much as possible and submit it at a time to improve efficiency.

 
From ZillaPress

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.