Php implode/explode, serialize, json, msgpack performance comparison, implodejson
Php implode/explode, serialize, json, msgpack Performance Comparison
First, use implode, serialize, json_encode, and msgpack_pack to create four text files for testing.
The creation code is as follows:
<? Php $ arr = array ('content1' => 'february 5, 1234, 80 or 90 123', 'content2' => 'february 5, 1234, 80 or 90 ', 'content3' => 'february 5, 1234, 80 or 90 '); echo file_put_contents('implode.txt', implode (',', $ arr), true ). '<br>'; echo file_put_contents('serialize.txt ', serialize ($ arr), true ). '<br>'; echo file_put_contents('json.txt ', json_encode ($ arr), true ). '<br>'; echo file_put_contents('msgpack.txt ', msgpack_pack ($ arr), true);?>
Generated after creation
Implode.txt 92 bytes
Serialize.txt 165 bytes
Json.txt 223 bytes
Msgpack.txt 121 bytes
The generated string size is sorted as follows: implode <msgpack_pack <serialize <json_encode
If the array is simple, json_encode may be smaller than serialize.
For example:
$ Arr = array ('1', '2', '3', '4', '5', '6', '7', '8 ', '9', '10 ');The value of serialize is 147 bytes.
Json_encode is 91 bytes
Comparison of implode, serialize, json_encode, and msgpack_pack Performance
<? Php $ arr = array ('content1' => 'february 5, 1234, 80 or 90 123', 'content2' => 'february 5, 1234, 80 or 90 ', 'content3' => 'february 5, 1234, 80 or 90 '); $ start = microtime (true); $ I = 1000000; while ($ I> 0) {// test the running time and memory usage respectively. $ tmp = implode (',', $ arr); // $ tmp = serialize ($ arr ); // $ tmp = json_encode ($ arr); // $ tmp = msgpack_pack ($ arr); $ I -- ;}$ end = microtime (true); echo 'run time: '. ($ end-$ start ).'s <br> '; echo 'memory usage :'. (apsaradb for memory_get_usage ()/1024 ). 'kb ';?>
implode 1.3225722312927s 628.50KBserialize 2.0553789138794s 628.32KBjson_encode 2.5058920383453s 628.34KBmsgpack_pack 1.6431028842926s 628.24KB
Result: The memory usage is similar to the running time.Implode <msgpack_pack <serialize <json_encode
Compare the performance of explode, unserialize, json_decode, and msgpack_unpack
<?php$data = file_get_contents('implode.txt');//$data = file_get_contents('serialize.txt');//$data = file_get_contents('json.txt');//$data = file_get_contents('msgpack.txt');$start = microtime(true);$i = 1000000;while($i>0){ $tmp = explode(',',$data); //$tmp = unserialize($data); //$tmp = json_decode($data, true); //$tmp = msgpack_unpack($data); $i--;}$end = microtime(true);echo 'run time:'.($end-$start).'s<br>';echo 'memory usage:'.(memory_get_usage()/1024).'KB';?>
explode 1.7446749210358s 628.74KBunserialize 2.1386790275574s 628.67KBjson_decode 5.2423169612885s 628.84KBmsgpack_unpack 2.2290098667145s 628.63KB
Result: The memory usage is similar. The runtime explode <serialize <msgpack_unpack <json_decode
To sum up, implode/explode is not suitable for complex structures, so serialize, json, and msgpack are commonly used.
The three types of comparisons, including running speed, memory usage, and space usage, are msgpack, serialize, and json.
If conditions are met,We recommend that you use msgpack to serialize and process data..
For more information about msgpack, see my previous article: MessagePack serialization format.