简体   繁体   中英

Efficient way to Parsing and manipulating string

I'm not sure i'm using the efficient way to manipulate large number of strings. I need to open a large file (~35kb) and parse and manipulate every line, ex:

a1="Name surname"
a2="Name surname"
a3="Name surname"
...
a800="Name surname"

I need to insert into a sqlite db every Name surname with the current id as index.

while ((s = d.readLine()) != null){
                        String[] name = s.split("\"");

                        String[] arrIdTemp = s.split("=");
                        String[] arrtId = arrIdTemp[0].split("a");

                        db.execSQL("INSERT INTO " + Constant.DB.DB_NAME
                                + " Values ('" + arrtId[1] + "','" + name[1]
                                + "');");

                    }

In this way i create 3 string arrays and for the first 100 lines is quite fast, but at the end the whole operation is slowing down, maybe because of a large use of memory? Is there a better solution to split and manipulate every line?

Logcat output says lots of:

01-27 14:46:50.554: D/dalvikvm(2541): GC_CONCURRENT freed 1778K, 68% free 3606K/11079K, external 4662K/5822K, paused 4ms+7ms

[1° SOLUTION]

I split the two operations and use transactions. From about 60 seconds, now i need only 2-3 seconds.

This is what i did:

//List of query
ArrayList<String> queryList = new ArrayList<String>();
...
while ((s = d.readLine()) != null){
                        String[] name = s.split("\"");

                        String[] arrIdTemp = s.split("=");
                        String[] arrtId = arrIdTemp[0].split("a");

                        queryList.add("INSERT INTO " + Constant.DB.DB_NAME
                                + " Values ('" + arrtId[1] + "','" + name[1]
                                + "');");

                    }

...

if (queryList!=null && queryList.size()>0){
                Iterator<String> iter = queryList.iterator();
                db.beginTransaction();
                try {
                    while (iter.hasNext()) {
                         db.execSQL(iter.next());
                        }
                    db.setTransactionSuccessful();
                } finally {
                    db.endTransaction();
                }
            }

if ther's a better solution or improvements on what i did, please tell me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM