php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51267 Performance of serialize() is poor compared to json_encode()
Submitted: 2010-03-11 01:30 UTC Modified: 2021-02-16 15:13 UTC
Votes:15
Avg. Score:4.2 ± 0.9
Reproduced:10 of 13 (76.9%)
Same Version:7 (70.0%)
Same OS:3 (30.0%)
From: jo at feuersee dot de Assigned: cmb (profile)
Status: Closed Package: Arrays related
PHP Version: 5.3.2 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jo at feuersee dot de
New email:
PHP Version: OS:

 

 [2010-03-11 01:30 UTC] jo at feuersee dot de
Description:
------------
The performance of serialize() decreases if the data is deeply nested, eg. consists of array inside an array.
Surprisingly, json_encode() is not affected by this issue, thus I'd say there should be some optimizations not used in serialize()

serialize() performs great on arrays structured (key => val), but fails ion structs like

array(
    array(key => val)
    ...
)

Test script:
---------------
Benchmark json_encode() vs. serialize() on the following data:

$arr = array();
for($i = 0; $i < $num_size; $i++) {
	$arr[md5($i)] = array(
		sha1($i),
		SplFixedArray::fromArray(
			array_fill(
				0, 10, hash('sha256', rand(0, $num_size))
			)
		)
	);
}


Expected result:
----------------
More or less the same same time comsumed.

Actual result:
--------------
json_encode() is >3 times faster than serialize().
This increases if the arrays nesting level increases.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-16 15:13 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2021-02-16 15:13 UTC] cmb@php.net
I just checked with PHP-7.4, and serialize() is actually slightly
faster than json_encode() now.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 07 17:01:33 2024 UTC