Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain perf difference in accessing SharedPreferences on different Android devices

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 articles about the "lag" 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!

like image 876
esilver Avatar asked Apr 09 '26 05:04

esilver


1 Answers

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

like image 181
Nikolay Elenkov Avatar answered Apr 11 '26 20:04

Nikolay Elenkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!