Performance test and analysis _php techniques for Json_encode, Json_decode and Serialize and Unserialize in PHP

Source: Internet
Author: User
So then Lenovo to the object in PHP how to serialize storage cost-effective? Then I thought about the JSON encoding and decoding functions recommended by my colleagues.
According to him, Json_encode and json_decode are more efficient than the built-in serialize and unserialize functions.
So I decided to experiment to see if what my colleague said was true.
The experiments were conducted in PHP 5.2.13 and PHP 5.3.2 environments.
Using the same variable, encode or decode it 10,000 times in the above way, and draw the time required for each function to execute 10,000 times.
The following is one of the test results for the PHP 5.2.13 environment:
Copy Code code as follows:

json:190
serialize:257
json_encode:0.08364200592041
json_decode:0.18004894256592
serialize:0.063642024993896
unserialize:0.086990833282471
Done.

The following is one of the test results for the PHP 5.3.2 environment:
Copy Code code as follows:

json:190
serialize:257
json_encode:0.062805891036987
json_decode:0.14239192008972
serialize:0.048481941223145
unserialize:0.05927300453186
Done.

The conclusion of this experiment is:
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.
Here's the code I used to do the test:
Copy Code code as follows:

<?php
$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: \t\t". Strlen ($json). "\ r \ n";
echo "Serialize: \ T". Strlen ($seri). "\r\n\r\n";
$stime = Microtime (true);
for ($i = 0; $i < 10000; $i + +)
{
Json_encode ($target);
}
$etime = Microtime (true);
echo "Json_encode: \ T". ($etime-$stime). "\ r \ n";
//----------------------------------
$stime = Microtime (true);
for ($i = 0; $i < 10000; $i + +)
{
Json_decode ($json);
}
$etime = Microtime (true);
echo "Json_decode: \ T". ($etime-$stime). "\r\n\r\n";
//----------------------------------
$stime = Microtime (true);
for ($i = 0; $i < 10000; $i + +)
{
Serialize ($target);
}
$etime = Microtime (true);
echo "Serialize: \ T". ($etime-$stime). "\ r \ n";
//----------------------------------
$stime = Microtime (true);
for ($i = 0; $i < 10000; $i + +)
{
Unserialize ($seri);
}
$etime = Microtime (true);
echo "Unserialize: \ T". ($etime-$stime). "\r\n\r\n";
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.