PHP Json_encode, Json_decode and Serialize, unserialize

Source: Internet
Author: User
Tags json php tutorial strlen

The efficiency of Json_encode and json_decode is not more efficient than serialize and unserialize, the performance is about twice times different when deserializing, PHP 5.3 performs more efficiently than PHP 5.2.

The code is as follows Copy Code


<?php Tutorial
$target = Array (
' Name ' => ' almighty helmet ',
' Quality ' => ' Blue ',
' ti_id ' => 21302,
' Is_bind ' => 1,
' Demand_conditions ' =>
Array (
' Herolevel ' => 1,
),
' Quality_attr_sign ' =>
Array (
' Herostrength ' => 8,
' Heroagility ' => 8,
' Herointelligence ' => 8,
),
);
$json = Json_encode ($target);
$seri = serialize ($target);
echo "JSON:". Strlen ($json). " ";
echo "Serialize:". Strlen ($seri). " ";
$stime = Microtime (true);
for ($i = 0; $i < 10000; $i + +)
{
Json_encode ($target);
}
$etime = Microtime (true);
echo "Json_encode:". ($etime-$stime). " ";
//----------------------------------
$stime = Microtime (true);
for ($i = 0; $i < 10000; $i + +)
{
Json_decode ($json);
}
$etime = Microtime (true);
echo "Json_decode:". ($etime-$stime). " ";
//----------------------------------
$stime = Microtime (true);
for ($i = 0; $i < 10000; $i + +)
{
Serialize ($target);
}
$etime = Microtime (true);
echo "Serialize:". ($etime-$stime). " ";
//----------------------------------
$stime = Microtime (true);
for ($i = 0; $i < 10000; $i + +)
{
Unserialize ($seri);
}
$etime = Microtime (true);
echo "Unserialize:". ($etime-$stime). " ";
Echo ' Done. '
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.