A Performance Tuning-Android-JSON Parsing & amp; Data

Source: Internet
Author: User

For some reason, we need to carry out a task that is composed of the following steps, in an Android Application.

Fetch web data from a Uri (cocould be in a stream)
Obtain a JSON Object out of the data in step [1].
Parse a Java object out of the JSON Object in step [2].
Extract some data (which is of quite a number, around 2 k tuples) out of the same data stream in step [1] and insert them all into the db.
With a quick and na implements ve implementation, I got the following on every step's time consumptions.

-FetchWeb (http: // *******/test_shop_meta.php) ends in 3.55 s
-JsonParser from stream ends in 20.92 s
-ClearAllRecords ends in 0.502 s
-Parse Shop. ShopCur ends in 0.051 s
-PourShopMetaIntoDB ends in 137.008 s

The step 160, 4 took about s, way too much. The heavy consumers are JSON Parsing and DB insertion.
After I correted the wrting to avoid open and close the writable db within the iteration, I got

-PourShopMetaIntoDB ends in 108.123 s

It made the performance better, but not radically.

 

After my turnning on the "transactional mode" of db operation, it was dramatically improved.

Db. setTransactionSuccessful ();
Db. endTransaction ();

-JsonParser from stream ends in 19.486 s
-PourShopMetaIntoDB ends in 18.001 s

On the other hand, I broke the "JSON parsing" into a more granual view:
-JsonParser from stream ends in 20.92 s
-Util. stringFromStream ends in 15.888 s
-New JSONObject (String) ends2.433s

The stream-> string operation took too long. Tweaking the buffer size in my buffer reader, which I used to read line by line from the stream, I got an improvement as this
-Util. stringFromStream ends in 5.214 s

Still takes too much. for the db inserting, it has to scan again the whole structure (no matter whether the string or the json object) To save the unnecessary double scanning, I tried a streaming parser instead of loading the whole string into json object and scanning the json object later again.

The GSON core streaming API is adopted since android SDK level-11 (named android. util. json ***, not the org. json), before that you need to download GSON lib and include it yourself.

After all that, I can finish the above mentioned task step 2, 3, 4 in a total 12 s now. (Step 1 depends on the network still .)



From TouchOn.asia

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.