json_encode / json_decode vs Zend_Json::encode / Zend_Json::decode
Do you know w开发者_如何学Chat's the best way both for performance and memory consuming ?
Thanks in advance.
Bye.
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.
$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
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).
精彩评论