Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_encode / json_decode vs Zend_Json::encode / Zend_Json::decode

Do you know what's the best way both for performance and memory consuming ?

Thanks in advance.

Bye.

like image 624
Aly Avatar asked Dec 09 '10 19:12

Aly


3 Answers

The only difference in functionality is as follows (per the Zend Framework docs):

When a method toJson() is implemented on an object to encode, Zend_Json calls this method and expects the object to return a JSON representation of its internal state.

Other than that there are no differences and it automatically chooses to use PHP's json_encode functionality if the json extension is installed. From their docs again:

If ext/json is not installed a Zend Framework implementation in PHP code is used for en-/decoding. This is considerably slower than using the PHP extension, but behaves exactly the same.

like image 152
mylesmg Avatar answered Nov 15 '22 04:11

mylesmg


$memoryNativeStart = memory_get_peak_usage (true);
$start = microtime( true );
$native = json_decode(json_encode( $data ));
$memoryNative =  memory_get_peak_usage (true) - $memoryNativeStart;
$jsonNativeTime = microtime( true ) - $start;
$msgNative = 'Native php <br>';
$msgNative .= 'time '.$jsonNativeTime.' memory '.$memoryNative.'<br>';

echo $msgNative;

sleep(3);

$memoryZendStart = memory_get_peak_usage (true);
$start = microtime( true );
$zend = Zend_Json::decode(Zend_Json::encode( $data ));
$memoryZend =  memory_get_peak_usage (true) - $memoryZendStart;
$jsonZendTime = microtime( true ) - $start;
$msgZend = 'Zend <br>';
$msgZend .= 'time '.$jsonZendTime.' memory '.$memoryZend;

echo $msgZend;

inside data there is about 130,000 records (with a result set)

I get

Native php

time 2.24236011505 memory 158072832

Zend

time 3.50552582741 memory 109051904
like image 28
Aly Avatar answered Nov 15 '22 04:11

Aly


Zend_Json is there so that it can be better integrated into an OO environment. As for performance, I would think json_encode/decode would be a bit faster, as they are built in functions (meaning they are not written in PHP).

like image 32
Jonah Avatar answered Nov 15 '22 03:11

Jonah