Read speed of SharedPreferences
Solution 1
Is there a way to put them in memory for reading?
They are in memory, after the first reference. The first time you retrieve a specific SharedPreferences
(e.g., PreferenceManager.getDefaultSharedPreferences()
), the data is loaded from disk, and kept around.
Solution 2
My advice is to test your performance first, and then start worrying about speed. In general, you'll be happier with an app that prioritizes maintainability as well as speed. When engineers start out to achieve performance before they get the app stable, the result is an app that runs a bit faster but has lots of bugs.
Solution 3
According to this link, getSharedPreferences
is not that much heavy because it opens file only when you call getSharedPreferences
first time:
// There are 1000 String values in preferences
SharedPreferences first = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// call time = 4 milliseconds
SharedPreferences second = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// call time = 0 milliseconds
SharedPreferences third = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// call time = 0 milliseconds
But using get
methods will take some time for the first time you call it:
first.getString("key", null)
// call time = 147 milliseconds
first.getString("key", null)
// call time = 0 milliseconds
second.getString("key", null)
// call time = 0 milliseconds
third.getString("key", null)
// call time = 0 milliseconds
GLee
Updated on August 03, 2022Comments
-
GLee almost 2 years
How fast are
SharedPreferences
? Is there a way to put them in memory for reading? I have a small amount of data that aListView
has to query to display each cell, and I'm worried that a call to flash memory will be too slow. I'm not worried about write speed, as writes will happen infrequently. I'm considering just using a JSON object to persist the data instead ofSharedPreferences
. Any thoughts? -
Mohammad Ersan over 10 yearsyou say, the whole saved data in
SharedPreferences
will be loaded once you get instance ofPreferenceManager.getDefaultSharedPreferences()
??? -
CommonsWare over 10 years@MoshErsan: Yes. The
SharedPreferences
is held in a staticHashMap
ofSharedPreferences
inContextImpl
. It will remain there until the process is terminated. SeesSharedPrefs
in github.com/android/platform_frameworks_base/blob/master/core/… -
Mohammad Ersan over 10 yearswhat is the benefit of doing it like this?!!
-
CommonsWare over 10 years@MoshErsan: Among other things, it ensures that all components of your app, asking for the same
SharedPreferences
, get the actual sameSharedPreferences
instance, and so they cannot get out of sync with each other. -
Mohammad Ersan over 10 yearsI was reading
SharedPreferencesImpl
->startLoadFromDisk
, in this case if we saved a big data in it, it may causeOOE
. -
CommonsWare over 10 years@MoshErsan: Correct.
SharedPreferences
should be small. -
user924 over 5 years@MohammadErsan for some data it's better to use database (SQLite) and SharedPreferences for some settings. I don't think your apps have a lot of settings
-
Ashton almost 4 yearsbad answer, you still have to load from disk, even you have the reference. Gosh!
-
CommonsWare almost 4 years@Devon: "you still have to load from disk" -- not according to the source code and the source code. Preferences and their values are cached.
-
Slion almost 3 yearsSo it looks like a "lazy“ implementation the loading happens on first get.
-
avisper over 2 yearsamazing article! thanks for sharing