开发者

Load Files for Effeiency [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I was told there is a way to clean up my code to make it run faster. i should be able to reuse variables so i wont have to keyed them over and over again. Thus in return should make my reading of my files faster when load time comes, but i cant seem to figure out how to do this. My code works fine just not as effiecient.

        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 = myContex开发者_Go百科t.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;


Your logic is a good candidate for factoring out into a subroutine, with the resource id as an argument.

That isn't going to improve the execution efficiency of the code. Things that are likely to help with efficiency are:

  1. Use SAX parsing (or pull parsing) instead of DOM parsing, and execute each statement as you get to its end.
  2. Turn on transaction mode, if applicable, and commit at the end of each file. (I don't know what SQL is being executed.)

In general, though, you need to profile your code to find out what's taking time. Then you can focus your optimization efforts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜