Home >Backend Development >PHP Tutorial >About php data serialization testing

About php data serialization testing

小云云
小云云Original
2018-01-26 14:14:161368browse

This article mainly introduces relevant information about the detailed explanation of PHP data serialization test examples. It mainly introduces the comparison of msgpack, json, and serialize. Friends in need can refer to it. I hope it can help everyone.

Detailed explanation of php data serialization test example

Test code


$msg = ['test'=>23];
$start = microtime(true); 
for($i=0;$i<100000;$i++){
  $packMsg = msgpack_pack($msg);
}
echo &#39;pack len:&#39;.strlen($packMsg)."\r\n";
$end = microtime(true);
echo &#39;run time:&#39;.($end-$start).&#39;s&#39;."\r\n"; 
echo &#39;memory usage:&#39;.(memory_get_usage()/1024)."KB\r\n";
/*
$start = microtime(true); 
for($i=0;$i<100000;$i++){
  $jsonMsg = json_encode($msg);
}
echo &#39;json len:&#39;.strlen($jsonMsg)."\r\n";
$end = microtime(true); 
echo &#39;run time:&#39;.($end-$start).&#39;s&#39;."\r\n"; 
echo &#39;memory usage:&#39;.(memory_get_usage()/1024)."KB\r\n";

$start = microtime(true); 
for($i=0;$i<100000;$i++){
  $packMsg = serialize($msg);
}
echo &#39;php len:&#39;.strlen($packMsg)."\r\n";
$end = microtime(true);
echo &#39;run time:&#39;.($end-$start)."s\r\n";
echo &#39;memory usage:&#39;.(memory_get_usage()/1024)."KB\r\n";*/

Execution results


##

pack len:7
run time:0.024219989776611s
memory usage:354.4765625KB
json len:11
run time:0.010890007019043s
memory usage:354.1796875KB
php len:22
run time:0.010586977005005s
memory usage:353.8828125KB

Analysis comments

The basic results found online are (estimated to be Versions before php7)


运行速度 serialize<json<msgpack
长度  serialize>json>msgpack
内存消耗 serialize<json<msgpack //不过近乎一致

is run in php7, and the results are as follows

##

运行速度 serialize<msgpack<json  //这里出现了变化
长度  serialize>json>msgpack
内存消耗 serialize<json<msgpack //不过近乎一致

Related recommendations:


jQuery form serialization example code example sharing

Detailed explanation of PHP's session deserialization vulnerability

Notes on jquery form serialization

The above is the detailed content of About php data serialization testing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn