Android - Strings.xml versus text files. Which is faster?

不问归期 提交于 2019-12-12 03:44:47

问题


I have a dictionary database which contains a total of 99,833 words but separated per letter. I am testing dictionaryA.txt which contains 3922 words.

Case 1:

When I enter a word I wanna look up, lets say "abacus", Using a buffered reader, My app says "Thesis is not responding. Wait - Quit". If I choose wait, it will return the word abacus and its definition.

CODE:

InputStream inputStream = getResources().openRawResource(R.raw.definitiona); 
                    try {
                        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
                        String s = in.readLine();
                        String delimiter = " - ";
                        String del[];
                        while(s != null)
                        {
                            s = in.readLine().replaceAll("\\s+", " ");
                            del = s.split(delimiter);
                            if (enhancedStem.equals(del[0]))
                            {
                                in.close();
                                databaseOutput.setText(s);
                                break;
                            }                        
                        }
                        in.close();
                        }
                    catch (FileNotFoundException e) {
                        databaseOutput.setText("" + e);
                    }
                    catch (IOException e1) {
                        databaseOutput.setText("" + e1);
                    }

Case 2:

When I enter a word I wanna look up, lets say "abacus", using the strings.xml (i transferred the whole 3922 words in a string-array), it says the same. I have to click wait before it responds.

CODE:

String[] wordA = getResources().getStringArray(R.array.DictionaryA);
                String delimiter = " - ";
                String[] del;
                for (int wordActr = 0; wordActr <= wordA.length - 1; wordActr++)
                {
                    String wordString = wordA[wordActr].toString();
                    del = wordString.split(delimiter);

                    if (enhancedStem.equals(del[0]))
                    {
                        databaseOutput.setText(wordA[wordActr]);
                        break;
                    }
                    else
                        databaseOutput.setText("Word not found!");

                }

Can you tell me which is better? Should I keep using my textfile? Or transfer them to the strings.xml since they both respond slowly anyway? And do you have any idea how I can eliminate the "Not responding" problem? Thanks in advance!


回答1:


The main advantages of using an Xml over a text file are

  1. If has a well defined structure, so parsing it is a easy task.
  2. You can also define schema for the xml and there by you can traverse through the xml pretty easily and it becomes type safe.
  3. Better performance.
  4. More reliable.

so its better to use xml ...




回答2:


You are encountering the wait message because you perform a heavy operation in the main thread. You can eliminate the wait message by using Threads or AsyncTask

Usage of strings.xml is recommended by Google for use cases similar to yours




回答3:


If you'd show some code, this would be easier. My first guess is that since your UI is not responding, you're doing the "search task" on the UI thread. Executing heavy tasks on the UI thread is generally not a good idea, instead you should look into AsyncTasks, Threads or something else, whatever suits best. By executing the task in the background, the user can still interact with your application - for example stop the search task if he has a typo in his search or simply does not care anymore. Until the search is done and the results are fetched, you can simply display some sort of wait-dialog.

Which one of your two suggestions is faster I can't tell. You could create two threads, both doing one way and measure the time.




回答4:


99,833 words can be stored in less than 2 MB

In that case the fastest is to keep in memory, so the case 3 :)



来源:https://stackoverflow.com/questions/15135371/android-strings-xml-versus-text-files-which-is-faster

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!