timeattack('JSON encoding (Object)', 100000, function () use($object) { // $ignore = json_encode($object); $ignore = \Json\Json::stringify($object); }); timeattack('JSON encoding (Array)', 100000, function () use($array) { // $ignore = json_encode($array); $ignore = \Json\Json::stringify($array); }); timeattack('JSON encoding (Null)', 100000, function () { // $ignore = json_encode(null); $ignore = \Json\Json::stringify(null); }); timeattack('JSON encoding (hasError)', 100000, function () use($fp) { // $ignore = json_encode(array($fp)); try { $ignore = \Json\Json::stringify(array($fp)); } catch (\Json\Exception\Encoding $e) { } }); echo "-------------------------\n"; echo "done.\n"; ob_start(function ($buffer) { return ''; }); ?> 5.5.27 ------------------------- JSON encoding (Object) * 100000: 0.51506591 sec JSON encoding (Array) * 100000: 0.27953506 sec JSON encoding (Null) * 100000: 0.05300689 sec
return @json_encode($var); }; } foreach (timeattack('Function-JSON encoding (Object)', 100000) as $_) { $ignore = $statement($php_object); } if (version_compare(PHP_VERSION, '5.4.0', '>=')) { $statement = function ($var) { return @json_encode($var, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); }; } else { $statement = function ($var) { return @json_encode($var); }; } foreach (timeattack('Function-JSON encoding (Array)', 100000) as $_) { $ignore = $statement($php_array); } echo "-------------------------\n"; echo "done.\n"; ob_start(function ($buffer) { return ''; }); ?> 5.5.27 ------------------------- JSON decoding (Object) * 100000: 0.96562314 sec JSON decoding (Array) * 100000: 0.78059888 sec JSON decoding (Null) * 100000: 0.09851217 sec
printf("%s * %d: %.8f sec\n", $label, $repeat, $time); } $object = '{"name":"Luke Skywalker","teacher":"Yoda","job":"Jedi","force":true,"height":1.72,"weight":77,"droid":["C-3PO","R2-D2"]}'; $array = '[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]'; echo PHP_VERSION . "\n"; echo "-------------------------\n"; timeattack('JSON decoding (Object)', 100000, function () use($object) { // $ignore = json_decode($object); $ignore = \Json\Json::parse($object); }); timeattack('JSON decoding (Array)', 100000, function () use($array) { // $ignore = json_decode($array); $ignore = \Json\Json::parse($array); }); timeattack('JSON decoding (Null)', 100000, function () { // $ignore = json_decode(''); $ignore = \Json\Json::parse(''); }); echo "-------------------------\n"; echo "done.\n"; ob_start(function ($buffer) { return ''; }); ?> 5.5.27 ------------------------- JSON decoding (Object) * 100000: 0.96562314 sec JSON decoding (Array) * 100000: 0.78059888 sec JSON decoding (Null) * 100000: 0.09851217 sec 5.5.27