simdjson_php 解析json利器推薦給你們

介紹

simdjson_php(github.com/crazyxman/s…)是一個php擴展,它綁定simdjson來實現快速解析,simdjson是一個高速的json解析器,它使用了大多數SIMD單一指令。simdjson介紹:github.com/lemire/simd…php

環境依賴

  • php7+
  • 帶有AVX2的處理器(即,2013年發佈的Haswell微體系結構的Intel處理器和2017年發佈的Zen微體系結構的AMD處理器),大多數cpu都是支持的
  • 最近的C ++編譯器(例如,GNU GCC或LLVM CLANG或Visual Studio 2017),咱們假設C ++ 17。GNU GCC 7或更高版本或LLVM的clang 6或更高版本
  • 檢查操做系統/處理器是否支持它:
    • OS X: sysctl -a | grep machdep.cpu.leaf7_features
    • Linux: grep avx2 /proc/cpuinfo

使用簡介

  1. 當須要獲取一個較大json串中的某個key時 使用simdjson_key_value() 是比較合適的,不像json_decode() 把整個json串解析成數組,開闢沒必要要的內存,固然在性能上是略遜於hash查找的。
  2. 當驗證一個字符串是否爲json時simdjson_isvaild() 是比較合適的,而且是很是快的,一樣不須要經過json_decode()來驗證。
//檢查字符串是否爲一個有效的json:
$isValid = simdjson_isvalid($jsonString); //return bool

//解析一個json字符串,返回數組,對象,null,相似json_decode(),第三個參數爲解析的深度
$parsedJSON = simdjson_decode($jsonString, true, 512); //return array|object|null. "null" string is not a standard json

/* { "Image": { "Width": 800, "Height": 600, "Title": "View from 15th Floor", "Thumbnail": { "Url": "http://www.example.com/image/481989943", "Height": 125, "Width": 100 }, "Animated" : false, "IDs": [116, 943, 234, 38793, {"p": "30"}] } } */

//注意. "\t" 是一個分割符. 它必須是一個控制字符. 它用來分割對象的key或數組的下標
//例如. "Image\tThumbnail\tUrl" 是正確. 'Image\tThumbnail\tUrl' 是錯誤的


//根據json串獲取指定key的值
$value = simdjson_key_value($jsonString, "Image\tThumbnail\tUrl");
var_dump($value); // string(38) "http://www.example.com/image/481989943"

$value = simdjson_key_value($jsonString, "Image\tIDs\t4", true);
var_dump($value); 
/* array(1) { ["p"]=> string(2) "30" } */

//獲取json解析後的資源,只解析一次,後續使用再也不解析
$resource = simdjson_resource($jsonString);
//根據json資源獲取指定key的值
$value = simdjson_key_value($resource, "Image\tThumbnail\tUrl");
var_dump($value); // string(38) "http://www.example.com/image/481989943"

$value = simdjson_key_value($resource, "Image\tIDs\t4", true);
var_dump($value); 
/* array(1) { ["p"]=> string(2) "30" } */

//檢查key是否存在,參數能夠是一個json串也能夠是一個json資源,返回true,false,null。當第一個參數是字符串時返回null表明解析失敗
$res = simdjson_key_exists($jsonString, "Image\tIDs\t1");
var_dump($res) //bool(true)
$res = simdjson_key_exists($resource, "Image\tIDs\t1");
var_dump($res) //bool(true)
複製代碼

性能測試(秒)

filename json_decode simdjson_decode simdjson_isvalid
apache_builds.json 0.00307300 0.00225200 0.00018100
canada.json 0.13955000 0.02773900 0.00358300
citm_catalog.json 0.03030900 0.01334000 0.00117000
github_events.json 0.00294100 0.00090400 0.00008500
gsoc-2018.json 0.04292500 0.01112000 0.00186700
instruments.json 0.00509700 0.00231800 0.00017500
marine_ik.json 0.09833600 0.04417500 0.00463400
mesh.json 0.01869200 0.00722600 0.00114800
mesh.pretty.json 0.03576200 0.00738100 0.00163400
numbers.json 0.00263600 0.00069900 0.00018200
random.json 0.01713500 0.00973900 0.00063000
twitter.json 0.01258600 0.00618400 0.00057400
twitterescaped.json 0.01435900 0.00650400 0.00074300
update-center.json 0.01506000 0.00869100 0.00047800

You may run the benchmarks by running the commands:git

  • php benchmark/benchmark.php

若有不足之處下方留言便可,歡迎你們批評指正,我會虛心接納。github

相關文章
相關標籤/搜索