Performance of json_encode, json_decode, serialize, and unserialize in PHP

Source: Internet
Author: User
Today, I accidentally wondered what the efficiency would be if I used PHP to write a small file-based Key-Value database similar to BDB for storing unstructured record-type data? JSON

As a result, I think about how PHP Objects are serialized and stored with the most cost-effective performance? Then I thought of the JSON encoding and decoding functions recommended by my colleagues.
According to him, json_encode and json_decode are more efficient than built-in serialize and unserialize functions.
So I decided to conduct an experiment to confirm whether my colleague's situation was true.
The experiment is performed in PHP 5.2.13 and PHP 5.3.2 respectively.
Use the same variable to encode or decode 10000 times and obtain the time required for each function to execute 10000 times.
The following is a test result in the PHP 5.2.13 environment:
The code is as follows:
Json: 190
Serialization: 257
Json_encode: 0.08364200592041
Json_decode: 0.18004894256592
Serialization: 0.063642024993896
Unserialized: 0.086990833282471
DONE.

One of the test results in the PHP 5.3.2 environment is as follows:
The code is as follows:
Json: 190
Serialization: 257
Json_encode: 0.062805891036987
Json_decode: 0.14239192008972
Serialization: 0.048481941223145
Unserialized: 0.05927300453186
DONE.

The experiment concluded that:
The efficiency of json_encode and json_decode is not higher than that of serialize and unserialize. during deserialization, the performance difference is about twice. the execution efficiency of PHP 5.3 is slightly higher than that of PHP 5.2.
The following code is used for testing:
The code is as follows:
$ Target = array (
'Name' => 'all-round 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". strlen ($ json). "\ r \ n ";
Echo "serialize: \ t". strlen ($ seri). "\ r \ n ";
$ Stime = microtime (true );
For ($ I = 0; I I <10000; $ I ++)
{
Json_encode ($ target );
}
$ Etime = microtime (true );
Echo "json_encode: \ t". ($ etime-$ stime). "\ r \ n ";
//----------------------------------
$ Stime = microtime (true );
For ($ I = 0; I I <10000; $ I ++)
{
Json_decode ($ json );
}
$ Etime = microtime (true );
Echo "json_decode: \ t". ($ etime-$ stime). "\ r \ n ";
//----------------------------------
$ Stime = microtime (true );
For ($ I = 0; I I <10000; $ I ++)
{
Serialize ($ target );
}
$ Etime = microtime (true );
Echo "serialize: \ t". ($ etime-$ stime). "\ r \ n ";
//----------------------------------
$ Stime = microtime (true );
For ($ I = 0; I I <10000; $ I ++)
{
Unserialize ($ seri );
}
$ Etime = microtime (true );
Echo "unserialize: \ t". ($ etime-$ stime). "\ 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.