Home  >  Article  >  Backend Development  >  Detailed explanation of examples of data serialization in php

Detailed explanation of examples of data serialization in php

黄舟
黄舟Original
2017-08-13 09:06:03940browse

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

php data serialization test examples. Detailed explanation

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

Analytical comments

The basic results of online searches are all (presumably versions before php7)

##

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

Run in php7, the results are as follows

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

The above is the detailed content of Detailed explanation of examples of data serialization in php. 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