SharedPreferences
有多快?有没有办法将它们放入内存中进行读取?我有少量数据需要ListView
查询才能显示每个单元格,我担心对闪存的调用会太慢。我不担心写入速度,因为写入不会频繁发生。我正在考虑只使用JSON对象来持久化数据,而不是SharedPreferences
。有什么想法吗?
发布于 2013-10-03 09:15:44
我的建议是先测试你的性能,然后再开始担心速度。一般来说,你会更喜欢一个既注重速度又注重可维护性的应用程序。当工程师在获得稳定的应用程序之前开始实现性能时,结果是应用程序运行得更快,但有很多错误。
发布于 2020-07-08 23:20:12
根据this链接,getSharedPreferences
并不是很重,因为它只在你第一次调用getSharedPreferences
时打开文件:
// 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
但是使用get
方法第一次调用它需要一些时间:
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
https://stackoverflow.com/questions/19148282
复制相似问题