編輯:Android編程入門
場景描寫敘述:
本文僅驗證了在安卓環境下使用Json的Key作為反序列化條件的解析速度。結論是解析速度最快的不是阿裡的fastjson,也不是Google的Gson,而是json-smart。
Android 4.4.2
fastjson-1.1.34.android.jar
gson-2.2.4.jar
json-smart-2.0-RC3.jar
**注意場景的限定條件**
核心代碼:
package com.h3c.mytestview; import java.io.StringReader; import net.minidev.json.JSONValue; import android.app.Activity; import android.os.Bundle; import android.util.Log; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.stream.JsonReader; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(new Runnable() { @Override public void run() { doSth(); } }).start(); } private void doSth() { Log.e("H6c","start..."); String json = "{\"name\":\"reiz\"}"; String k = ""; try { long startTime1 = System.currentTimeMillis(); for(int n = 0;n < 100000; n++) { org.json.JSONObject jo; jo = new org.json.JSONObject(json); k = jo.getString("name"); } long endTime1 = System.currentTimeMillis() - startTime1; Log.e("H6c","android:"+ endTime1); long startTime2 = System.currentTimeMillis(); for(int n = 0;n < 100000; n++) { JSONObject jo = JSON.parseObject(json); k = jo.getString("name"); } long endTime2 = System.currentTimeMillis() - startTime2; Log.e("H6c","fastjson:"+ endTime2); long startTime3 = System.currentTimeMillis(); for(int n = 0;n < 100000; n++) { net.minidev.json.JSONObject jo = (net.minidev.json.JSONObject)JSONValue.parseStrict(json); k = (String)jo.get("name"); } long endTime3 = System.currentTimeMillis() - startTime3; Log.e("H6c","json-smart:"+endTime3); long startTime4 = System.currentTimeMillis(); for(int n = 0;n < 100000; n++) { JsonElement je = new JsonParser().parse(json); JsonObject jo = je.getAsJsonObject(); k = jo.get("name").getAsString(); } long endTime4 = System.currentTimeMillis() - startTime4; Log.e("H6c","gson:"+endTime4); } catch (Exception e) { e.printStackTrace(); } } }
測試結果:
Galaxy S3
===========Galaxy S3=============1 05-22 11:04:17.120: E/H6c(28681): start... 05-22 11:04:21.525: E/H6c(28681): android:4400 05-22 11:04:28.135: E/H6c(28681): fastjson:6608 05-22 11:04:31.600: E/H6c(28681): json-smart:3467 05-22 11:04:47.440: E/H6c(28681): gson:15839 ===========Galaxy S3=============2 05-22 11:05:08.230: E/H6c(28681): start... 05-22 11:05:12.120: E/H6c(28681): android:3886 05-22 11:05:17.285: E/H6c(28681): fastjson:5167 05-22 11:05:21.020: E/H6c(28681): json-smart:3736 05-22 11:05:34.040: E/H6c(28681): gson:13018 ===========Galaxy S3=============3 05-22 11:05:45.440: E/H6c(28681): start... 05-22 11:05:49.470: E/H6c(28681): android:4033 05-22 11:05:54.500: E/H6c(28681): fastjson:5031 05-22 11:05:58.240: E/H6c(28681): json-smart:3736 05-22 11:06:13.485: E/H6c(28681): gson:15245
===========Galaxy S4=============1 05-22 10:58:03.541: E/H6c(26600): start... 05-22 10:58:06.934: E/H6c(26600): android:3393 05-22 10:58:12.680: E/H6c(26600): fastjson:5752 05-22 10:58:15.232: E/H6c(26600): json-smart:2549 05-22 10:58:24.841: E/H6c(26600): gson:9610 ===========Galaxy S4=============2 05-22 11:00:51.675: E/H6c(26600): start... 05-22 11:00:54.878: E/H6c(26600): android:3209 05-22 11:01:00.413: E/H6c(26600): fastjson:5526 05-22 11:01:02.986: E/H6c(26600): json-smart:2569 05-22 11:01:11.084: E/H6c(26600): gson:8099 ===========Galaxy S4=============3 05-22 11:01:31.213: E/H6c(26600): start... 05-22 11:01:34.717: E/H6c(26600): android:3508 05-22 11:01:40.172: E/H6c(26600): fastjson:5443 05-22 11:01:42.734: E/H6c(26600): json-smart:2566 05-22 11:01:52.554: E/H6c(26600): gson:9823
===========Xiaomi 3=============1 05-22 11:02:05.461: E/H6c(30473): start... 05-22 11:02:08.021: E/H6c(30473): android:2561 05-22 11:02:11.781: E/H6c(30473): fastjson:3766 05-22 11:02:13.411: E/H6c(30473): json-smart:1629 05-22 11:02:16.811: E/H6c(30473): gson:3402 ===========Xiaomi 3=============2 05-22 11:02:30.291: E/H6c(30473): start... 05-22 11:02:32.481: E/H6c(30473): android:2184 05-22 11:02:36.341: E/H6c(30473): fastjson:3856 05-22 11:02:37.781: E/H6c(30473): json-smart:1439 05-22 11:02:40.991: E/H6c(30473): gson:3210 ===========Xiaomi 3=============3 05-22 11:02:47.731: E/H6c(30473): start... 05-22 11:02:50.271: E/H6c(30473): android:2548 05-22 11:02:53.831: E/H6c(30473): fastjson:3553 05-22 11:02:55.261: E/H6c(30473): json-smart:1431 05-22 11:02:58.571: E/H6c(30473): gson:3310
在使用key解析json的場景下,速度最快的是json-smart,其解析速度有所提升。
本文研究的結論是Android原生Java解析已經具備基礎json解析能力,並且在速度上有一定的優勢,在簡單的場景下無需引用噱頭比較大的fastjson和gson。並且在對速度沒有極端追求的場景下也無需引入json-smart, 由於其可能只只幫你節省了後台1s的時間,感官上事實上察覺不明顯。
一、ArrayAdapter 只顯示文字activitylistview_layout.xml<?xml version=1.0 encoding=utf-8?&g
activity_ui6.xml<?xml version=1.0 encoding=utf-8?><GridView xmlns:android=ht
上一篇上一篇介紹了完成Android輸入法的最小化步驟,它只能將按鍵對應的字符上屏。一般的東亞語言都有一個轉換的過程,比如漢語輸入拼音,需要由拼音轉成漢字再上屏。本文將在
生活中我們常認定某些人很有才,但什麼是有才呢?明朝的王守仁曾這樣解釋:才,是所謂天理,應用到物上,便成了才。凡事凡物,只要