2011-10-02 102 views
-1

我被告知有一種方法可以清理我的代碼以使其運行速度更快。我應該能夠重用變量,所以我不必一次又一次地鍵入它們。因此,作爲回報,當加載時間到來時應該讓我更快地閱讀我的文件,但我似乎無法弄清楚如何做到這一點。我的代碼工作得很好,但效率不高。加載文件以提高效率

 InputStream in = myContext.getResources().openRawResource(R.raw.sql1); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     String xmlFile = br.readLine(); 
     DocumentBuilder builder =   DocumentBuilderFactory.newInstance().newDocumentBuilder(); 

     Document doc = builder.parse(xmlFile); 

     NodeList statements = doc.getElementsByTagName("statement");    

     for (int i=0; i<statements.getLength(); i++) { 
      s = statements.item(i).getChildNodes().item(0).getNodeValue(); 
      db.execSQL(s); 
     } 

     in.close(); 
     doc = null; 
     statements = null; 
     in = null; 
     br.close(); 
     br = null; 


     InputStream in2 = myContext.getResources().openRawResource(R.raw.sql2); 
     BufferedReader br2 = new BufferedReader(new InputStreamReader(in2)); 
     String xmlFile2 = br2.readLine(); 
     Document doc2 = builder.parse(xmlFile2); 

     NodeList statements2 = doc2.getElementsByTagName("statement");   

     for (int i=0; i<statements2.getLength(); i++) { 
      s = statements2.item(i).getChildNodes().item(0).getNodeValue(); 
      db.execSQL(s); 
     } 

     in2.close(); 
     doc2 = null; 
     statements2 = null; 
     in2 = null; 
     br2.close(); 
     br2 = null; 




     InputStream in3 = myContext.getResources().openRawResource(R.raw.sql3); 
     BufferedReader br3 = new BufferedReader(new InputStreamReader(in3)); 
     String xmlFile3 = br3.readLine(); 

     Document doc3 = builder.parse(xmlFile3); 

     NodeList statements3 = doc3.getElementsByTagName("statement");   
     for (int i=0; i<statements3.getLength(); i++) { 
      s = statements3.item(i).getChildNodes().item(0).getNodeValue(); 
      db.execSQL(s); 
     } 

     in3.close(); 
     doc3 = null; 
     statements3 = null; 
     in3 = null; 
     br3.close(); 
     br3 = null; 


     InputStream in4 = myContext.getResources().openRawResource(R.raw.sql4); 
     BufferedReader br4 = new BufferedReader(new InputStreamReader(in4)); 
     String xmlFile4 = br4.readLine(); 
     Document doc4 = builder.parse(xmlFile4); 
     NodeList statements4 = doc4.getElementsByTagName("statement");   
     for (int i=0; i<statements4.getLength(); i++) { 
      s = statements4.item(i).getChildNodes().item(0).getNodeValue(); 
      db.execSQL(s); 
     } 

     in4.close(); 
     doc4 = null; 
     statements4 = null; 
     in4 = null; 
     br4.close(); 
     br4 = null; 

     InputStream in5 = myContext.getResources().openRawResource(R.raw.sql5); 
     BufferedReader br5 = new BufferedReader(new InputStreamReader(in5)); 
     String xmlFile5 = br5.readLine(); 
     Document doc5 = builder.parse(xmlFile5); 
     NodeList statements5 = doc5.getElementsByTagName("statement");   
     for (int i=0; i<statements5.getLength(); i++) { 
      s = statements5.item(i).getChildNodes().item(0).getNodeValue(); 
      db.execSQL(s); 
     } 

     in5.close(); 
     doc5 = null; 
     statements5 = null; 
     in5 = null; 
     br5.close(); 
     br5 = null; 

回答

0

您的邏輯是一個很好的選擇,將資源id作爲參數分解爲子例程。

這不會提高代碼的執行效率。可能對提高效率有幫助的事情如下:

  1. 使用SAX解析(或拉解析)而不是DOM解析,並在達到其結尾時執行每條語句。
  2. 打開事務模式(如果適用),並在每個文件結尾提交。 (我不知道SQL執行的是什麼)。

但是,一般來說,您需要對您的代碼進行配置以瞭解需要花費的時間。然後,您可以專注於優化工作。