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. '
?>