65019

Explain perf difference in accessing SharedPreferences on different Android devices

Question:

I am trying to debug extreme performance differences for users of my Android app. I have traced it to extreme differences in DB write time and SharedPreferences read and write time.

Here are 7 performance tests I wrote to test the speed of SharedPreferences:

private void testEditor1() { SharedPreferences settings = this.getSharedPreferences(Constants.PrefsName, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("testEditor1", true); editor.commit(); } private void testEditor2() { for (int i = 0; i < 10; i++) { SharedPreferences settings = this.getSharedPreferences(Constants.PrefsName, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("testEditor2", true); editor.commit(); } } private void testEditor3() { SharedPreferences settings = this.getSharedPreferences(Constants.PrefsName, 0); SharedPreferences.Editor editor = settings.edit(); editor.putInt("testEditor3", 1); editor.commit(); } private void testEditor4() { for (int i = 0; i < 10; i++) { SharedPreferences settings = this.getSharedPreferences(Constants.PrefsName, 0); SharedPreferences.Editor editor = settings.edit(); editor.putInt("testEditor4", i); editor.commit(); } } private void testEditor5() { for (int i = 0; i < 100; i++) { SharedPreferences settings = this.getSharedPreferences(Constants.PrefsName, 0); SharedPreferences.Editor editor = settings.edit(); editor.putInt("testEditor5", i); editor.commit(); } } private void testEditor6() { SharedPreferences settings = this.getSharedPreferences(Constants.PrefsName, 0); for (int i = 0; i < 10; i++) { SharedPreferences.Editor editor = settings.edit(); editor.putInt("testEditor6", i); editor.commit(); } } private void testEditor7() { SharedPreferences settings = this.getSharedPreferences(Constants.PrefsName, 0); for (int i = 0; i < 100; i++) { SharedPreferences.Editor editor = settings.edit(); editor.putInt("testEditor7", i); editor.commit(); } }

On my device (an Original Motorola droid), here are the results:

Test 1 (Write 1 bool): 21 ms Test 2 (Write 10 bools): 316 ms Test 3 (Write 1 int): 15 ms Test 4 (Write 10 ints): 182 ms Test 5 (Write 100 ints): 1525 ms Test 6 (Write 10 ints 2): 108 ms Test 7 (Write 100 ints 2): 1378 ms

On my colleague's device (an HTC Evo), here are the results:

Test 1 (Write 1 bool): 63 ms Test 2 (Write 10 bools): 14 ms Test 3 (Write 1 int): 15 ms Test 4 (Write 10 ints): 186 ms Test 5 (Write 100 ints): 919 ms Test 6 (Write 10 ints 2): 60 ms Test 7 (Write 100 ints 2): 823 ms

On one of our beta users' device, a Samsung Galaxy S, here are the results:

Test 1 (Write 1 bool): 1188 ms Test 2 (Write 10 bools): 1024 ms Test 3 (Write 1 int): 105 ms Test 4 (Write 10 ints): 1019 ms Test 5 (Write 100 ints): 8142 ms Test 6 (Write 10 ints 2): 630 ms Test 7 (Write 100 ints 2): 6610 ms

We see slow access times for both reads and writes. These numbers are generally consistent on the devices. The slow writes to SharedPreferences on our beta users device in some cases appear to take ~10 sec (10000 ms) for a single boolean value write when I inspect our startup processes.

Can anyone hypothesize why SharedPreferences perform so much more slowly on a Samsung Galaxy S? There are <a href="http://droid-den.com/phone-guides/phone-guide-how-to-root-the-samsung-galaxy-s-and-fix-lag/" rel="nofollow">articles</a> <a href="http://www.tiagoespinha.net/2010/08/samsung-galaxy-s-lag-issue-and-how-to-fix-it/" rel="nofollow">about</a> <a href="http://www.groovypost.com/howto/mobile/most-common-lag-fixes-for-samsung-galaxy-s-captivate-vibrant-fascinate-epic-4g/" rel="nofollow">the "lag"</a> on Samsung Galaxy S's out on the internet; would this explain the SharedPreferences behavior we see? If so, would someone mind explaining precisely how?

Thanks!

Answer1:

Writing to disk is slow on a stock Galaxy S. Shared preferences are saved to disk, as are DB files. Connect the dots :)

Answer2:

I seem like each time commits to SharedReference, Android open a new OutputStream to write to file then close it. the Source code should be changed like this:

private void testEditor6() { SharedPreferences settings = this.getSharedPreferences(Constants.PrefsName, 0); for (int i = 0; i < 10; i++) { SharedPreferences.Editor editor = settings.edit(); editor.putInt("testEditor6", i); //editor.commit(); } editor.commit(); }

Recommend

  • How to properly get little-endian integer in java
  • Fragment state lost when device left idle
  • ANDROID - java.lang.IllegalStateException: cannot perform this action after onSaveInstanceState
  • java.lang.ClassNotFoundException: com.mysql.jdbc.Driver (in jre's libs)
  • How to detect airplane mode especially in jelly beam?
  • Set focus to first invalid form element in AngularJS
  • Python delete lines of text line #1 till regex
  • jQuery Orbit - How to make a Random Slideshow?
  • Can't delete or rename original file after resizing
  • Select options in sencha touch is not working for android
  • Django invalid literal for int() with base 10
  • ListItem.Attributes.Add not working
  • Diff between two dataframes in pandas
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • How to use RequestBodyAdvice
  • Jetty Server not starting: Unable to establish loopback connection
  • Body moving without any force applied? (Box2d)
  • does jqgrid support a multiple checkbox list for editing
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • JFileChooser in front of fullscreen Swing application
  • jQuery show() function is not executed in Safari if submit handler returns true
  • Align navbar back button on right side
  • HTML download movie download link
  • Modifying destination and filename of gulp-svg-sprite
  • How to handle AllServersUnavailable Exception
  • How to model a transition system with SPIN
  • VBA Convert delimiter text file to Excel
  • ORA-29908: missing primary invocation for ancillary operator
  • How to disable jQuery.jplayer autoplay?
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal