Read speed of SharedPreferences

前端 未结 4 1711
庸人自扰
庸人自扰 2020-12-09 15:06

How fast are SharedPreferences? Is there a way to put them in memory for reading? I have a small amount of data that a ListView has to query to d

4条回答
  •  旧时难觅i
    2020-12-09 15:56

    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 
    

提交回复
热议问题