Bug #51267 [Opn->Csd]: Performance of serialize() is poor compared to json_encode()

From: Date: Tue, 16 Feb 2021 15:13:45 +0000
Subject: Bug #51267 [Opn->Csd]: Performance of serialize() is poor compared to json_encode()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-232202@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=51267&edit=1 ID: 51267 Updated by: cmb@php.net Reported by: jo at feuersee dot de Summary: Performance of serialize() is poor compared to json_encode() -Status: Open +Status: Closed Type: Bug Package: Arrays related PHP Version: 5.3.2 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: I just checked with PHP-7.4, and serialize() is actually slightly faster than json_encode() now. Previous Comments: ------------------------------------------------------------------------ [2010-03-11 01:30:26] 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. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=51267&edit=1

« previous php.bugs (#232202) next »