2013-03-16 34 views
-2

我正在为android设备制作一个应用程序。我的应用程序中有一个函数,它有2个for循环,每个迭代3200次,访问一个53 KB的.txt文件(其中包含3200行),并将字符串与每行进行比较,每次迭代一行。 “for循环”还包含BufferedReader(),InputStreamReader(),InputStream()和StringTokenizer()。所以当我在模拟器上运行应用程序时,它需要大约8秒的时间处理该功能。这是不可接受的。我该如何缩短所需时间,例如半秒或最大时间。 1秒?谢谢! 编辑:这是我的程序与2 for循环的一部分:如何让for()循环花费更少的时间(android)?

else if(a==2){ 
     String z=""; 
     try{ 
      InputStream is = getAssets().open("USCOUNTIES.txt"); 
      InputStreamReader iz=new InputStreamReader(is); 
      BufferedReader bis = new BufferedReader(iz); 

      int v=0; 

      v=count("USCOUNTIES.txt");//counts number of lines in the .txt file 
     //finding no. of counties to be displayed 
      int counter=0; 
      String pos; 
      pos=Integer.toString(position); 
      try{ 
      for(int i=0;i<v;i++){ 
       z=bis.readLine(); 
       //int x=pos.length(); 
       boolean a; 
       //using stringtokenizer 
       StringTokenizer st = new StringTokenizer(z, ","); 
       String substring; 
       substring=(String) st.nextElement(); 
       a=substring.equals(pos); 
       if(a==true){ 

        counter=counter+1; 

       } 
      }}catch(Exception e){e.printStackTrace();} 
      String array1[]=new String[counter]; 

      try{ 
       InputStream ig = getAssets().open("USCOUNTIES.txt"); 
       InputStreamReader ia=new InputStreamReader(ig); 
       BufferedReader bos = new BufferedReader(ia); 
      int j=0; 
      for(int i=0;i<v;i++){ 
       z=bos.readLine(); 
       String[] split = z.split(","); 
       if(split[0].equals(pos)){ 
        array1[j]=split[1]; 
        j=j+1; 
       } 

      }} 
      catch(Exception e){e.printStackTrace();} 
+1

老实说,8秒钟读取3200个文件,每行3200行是相当不错的。 – 2013-03-16 21:04:55

+0

它是一个单一的3200内衬文件。它迭代3200次。你可能有什么建议?我这样做是为了将一些文件的数据放在列表中。 – 2013-03-16 21:08:21

+7

为什么你重新阅读相同的文件** 3200次**? – CommonsWare 2013-03-16 21:08:56

回答

1

如果我是你,我会分析一切只是一个时间,然后用它做任何你想要的。

这段代码就是这样做的,包括Integers的解析(我怀疑你需要这些作为值,而不是Strings):

public void read() throws IOException { 
    InputStream is = getAssets().open("USCOUNTIES.txt"); 
    InputStreamReader iz=new InputStreamReader(is); 
    BufferedReader bis = new BufferedReader(iz); 
    String line = ""; 
    String firstNumber = ""; 
    String secondNumber = ""; 
    String countyName = ""; 
    StringTokenizer st = null; 
    HashMap<Pair, String> map = new HashMap<>(); 
    while((line = bis.readLine()) != null) { 
     st = new StringTokenizer(line, ","); 
     firstNumber = (String) st.nextElement(); 
     st = new StringTokenizer((String)st.nextElement(), ">"); 
     secondNumber = (String) st.nextElement(); 
     countyName = ((String) st.nextElement()); 
     countyName = countyName.substring(0, countyName.length()-1); 
     int num1 = Integer.parseInt(firstNumber); 
     int num2 = Integer.parseInt(secondNumber); 
     map.put(new Pair(num1, num2), countyName); 
    } 
} 

class Pair { 
    int num1, num2; 
    Pair(int num1, int num2) { 
     this.num1 = num1; 
     this.num2 = num2; 
    } 

    public boolean equals(Object other) { 
     if (other instanceof Pair) { 
      Pair np = (Pair) other; 
      return this.num1 == np.num1 && this.num2 == np.num2; 
     } 
     return false; 
    } 

    public int hashCode() { 
     return (Integer.valueOf(num1).hashCode() >> 13)^Integer.valueOf(num2).hashCode(); 
    }; 
} 

现在,你可以简单地检索每个countyName这一行:

String s = map.get(new Pair(1,69)); 

并返回Aleutians East

我希望得到你开始。

EDIT

这段代码使用2D SparseArray(很像HashMap<Integer, Object>)。有了这个,所有东西都按第一个数字排序。

public class Reader { 
    private String firstNumber = ""; 
    private String secondNumber = ""; 
    private String countyName = ""; 
    private StringTokenizer stringTokenizer = null; 
    private SparseArray<SparseArray<String>> sparseArray = new SparseArray<SparseArray<String>>(); 
    private SparseArray<String> temporarySparseArray = null; 

    public void readFromIS() throws IOException { 
     InputStream is = getAssets().open("USCOUNTIES.txt"); 
     InputStreamReader iz=new InputStreamReader(is); 
     BufferedReader bis = new BufferedReader(iz); 
     String line = null; 
     while((line = bis.readLine()) != null) { 
      readLine(line); 
     } 
    } 

    public void readFromList() { 
     String[] strings = { 
       "0,1>Autauga;", 
       "0,2>Baldwin;", 
       "0,3>Barbour;", 
       "1,69>Aleutians East;",  
       "1,68>Aleutians West;" 
     }; 
     for (String line : strings) { 
      readLine(line); 
     } 
    } 

    private void readLine(String line) { 
     stringTokenizer = new StringTokenizer(line, ","); 
     firstNumber = (String) stringTokenizer.nextElement(); 
     stringTokenizer = new StringTokenizer((String)stringTokenizer.nextElement(), ">"); 
     secondNumber = (String) stringTokenizer.nextElement(); 
     countyName = ((String) stringTokenizer.nextElement()); 
     countyName = countyName.substring(0, countyName.length()-1); 
     int num1 = Integer.parseInt(firstNumber); 
     int num2 = Integer.parseInt(secondNumber); 
     if (sparseArray.get(num1) == null) { 
      sparseArray.put(num1, new SparseArray<String>()); 
     } 
     temporarySparseArray = sparseArray.get(num1); 
     temporarySparseArray.put(num2, countyName); 
     sparseArray.put(num1, temporarySparseArray); 
     temporarySparseArray = null; 
    } 

    public void test() { 
     readFromList(); 
     String s = sparseArray.get(0).get(2); 
     SparseArray sa = sparseArray.get(0); 
     System.out.println(sa.size()); //should be 3 
     System.out.println(s); // should be Baldwin 
    } 
} 

并检索与num1开始所有的县,说,0,你只需要使用:

SparseArray<String> startingWithZero = sparseArray.get(0); 

FYI:一个SparseArrayintegers一个HashMap,所以不是一切都要autoboxed(从Integerint,因为您不能将原始类型放入HashMap)。

EDIT2您打印1D sparseArray的地址。

public void printEverythingStartingWithZero() { 
    SparseArray<String> subSparseArray = sparseArray.get(0); //You first need a 1D sparseArray 
    int key = 0; 
    for(int i = 0; i < subSparseArray.size(); i++) { 
     key = subSparseArray.keyAt(i); 
     String county = subSparseArray.get(key); //county is the String in place (0,key) 
     System.out.println(county); 
    } 
} 

您需要首先检索1D sparseArray,并且前导零。

+0

非常感谢这段代码!我有个问题。如果我想返回所有具有num1 = 1的字符串,例如String s = map.get(new Pair(1,i)),其中i来自for循环并从0到3200进行迭代。是否有更好的实现这一目标的方式? – 2013-03-17 09:59:06

+0

您必须以任一方式循环,因为您需要整个列表的一个子集。但也许可以用2D'sparseArray'。勒姆检查。 – stealthjong 2013-03-17 10:21:49

+0

这是可能的,看到我的补充。 – stealthjong 2013-03-17 11:03:46