Detailed explanation of php data serialization testing

墨辰丷
Release: 2023-03-26 16:46:01
Original
1610 people have browsed it

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";*/
Copy after login

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
Copy after login

Analysis and comments

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

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

Run in php7, the results are as follows

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

Related recommendations:

Use native js to implement form formSerialization (graphic tutorial)

##PHP Serialization and reverseSerializationDetailed explanation of function examples

JSONSerialization and reverseSerializationImplementation method (with code)

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!