Android解析Json速度最快的库:json-smart
最后更新 2021-02-28 17:35 星期日 所属:
安卓教程 浏览:570
场景描写描述:
文中仅认证了在安卓系统自然环境下应用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("H7c","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("H7c","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("H7c","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("H7c","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("H7c","gson:" endTime4); } catch (Exception e) { e.printStackTrace(); } } }
測试結果:
Galaxy S3
===========Galaxy S3=============1 05-22 11:04:17.120: E/H7c(28681): start... 05-22 11:04:21.525: E/H7c(28681): android:4400 05-22 11:04:28.135: E/H7c(28681): fastjson:6608 05-22 11:04:31.600: E/H7c(28681): json-smart:3467 05-22 11:04:47.440: E/H7c(28681): gson:15839 ===========Galaxy S3=============2 05-22 11:05:08.230: E/H7c(28681): start... 05-22 11:05:12.120: E/H7c(28681): android:3886 05-22 11:05:17.285: E/H7c(28681): fastjson:5167 05-22 11:05:21.020: E/H7c(28681): json-smart:3736 05-22 11:05:34.040: E/H7c(28681): gson:13018 ===========Galaxy S3=============3 05-22 11:05:45.440: E/H7c(28681): start... 05-22 11:05:49.470: E/H7c(28681): android:4033 05-22 11:05:54.500: E/H7c(28681): fastjson:5031 05-22 11:05:58.240: E/H7c(28681): json-smart:3736 05-22 11:06:13.485: E/H7c(28681): gson:15245
Galaxy S4
===========Galaxy S4=============1 05-22 10:58:03.541: E/H7c(26600): start... 05-22 10:58:06.934: E/H7c(26600): android:3393 05-22 10:58:12.680: E/H7c(26600): fastjson:5752 05-22 10:58:15.232: E/H7c(26600): json-smart:2549 05-22 10:58:24.841: E/H7c(26600): gson:9610 ===========Galaxy S4=============2 05-22 11:00:51.675: E/H7c(26600): start... 05-22 11:00:54.878: E/H7c(26600): android:3209 05-22 11:01:00.413: E/H7c(26600): fastjson:5526 05-22 11:01:02.986: E/H7c(26600): json-smart:2569 05-22 11:01:11.084: E/H7c(26600): gson:8099 ===========Galaxy S4=============3 05-22 11:01:31.213: E/H7c(26600): start... 05-22 11:01:34.717: E/H7c(26600): android:3508 05-22 11:01:40.172: E/H7c(26600): fastjson:5443 05-22 11:01:42.734: E/H7c(26600): json-smart:2566 05-22 11:01:52.554: E/H7c(26600): gson:9823
Xiaomi3
===========Xiaomi 3=============1 05-22 11:02:05.461: E/H7c(30473): start... 05-22 11:02:08.021: E/H7c(30473): android:2561 05-22 11:02:11.781: E/H7c(30473): fastjson:3766 05-22 11:02:13.411: E/H7c(30473): json-smart:1629 05-22 11:02:16.811: E/H7c(30473): gson:3402 ===========Xiaomi 3=============2 05-22 11:02:30.291: E/H7c(30473): start... 05-22 11:02:32.481: E/H7c(30473): android:2184 05-22 11:02:36.341: E/H7c(30473): fastjson:3856 05-22 11:02:37.781: E/H7c(30473): json-smart:1439 05-22 11:02:40.991: E/H7c(30473): gson:3210 ===========Xiaomi 3=============3 05-22 11:02:47.731: E/H7c(30473): start... 05-22 11:02:50.271: E/H7c(30473): android:2548 05-22 11:02:53.831: E/H7c(30473): fastjson:3553 05-22 11:02:55.261: E/H7c(30473): json-smart:1431 05-22 11:02:58.571: E/H7c(30473): gson:3310
从測试結果看来:
在应用key分析json的情景下,速率更快的是json-smart,其分析速率有一定的提高。
文中科学研究的结果是Android原生态Java分析早已具有基本json分析工作能力,而且在速率上面有一定的优点,在简易的情景下不用引入营销手段比較大的fastjson和gson。而且在对速率沒有极端化追求完美的情景下也不用引入json-smart, 因为其很有可能只只帮你节约了后台管理1s的時间,感观上实际上发觉不显著。