有了 Swoole 之後,用咱們熟悉的 PHP 就能夠很方便地開發網絡通訊應用。有時候咱們系統內部須要交換數據,那麼,這時候問題來了,網絡通信的數據格式是選擇 JSON
仍是 serialize
呢?php
JSON
顯然更通用,不用關心通訊雙方是什麼語言。json
serialize
也不差,它能夠將對象狀態保存,在反序列化時恢復狀態。這是 JSON
所不能比的。可是每種語言自身的 serialize
幾乎都不通用,跨語言通信行不通。api
因此,這裏咱們暫時只先考慮 PHP
內部系統間的數據交換,用哪一個更好呢?數組
這個問題,沒有親自測試過的人,確定不能拍着胸脯說出答案。由於我也看了,網上對這兩種數據格式公說紛紜。swoole
那麼,我也來親自驗證一下吧。固然,結果僅供參考,請根據實際場景選擇適合大家項目的數據格式。網絡
首先我要說明一下,我選擇了幾種數據結構,有數組、嵌套數組、對象、對象數組、某API大量數據,儘管挺多的,可是仍是有可能考慮不全面。數據結構
首先我使用了一個公開api來獲取一個大量數據,保存爲data.json
文件,api地址:https://www.apiopen.top/journalismApi性能
個人環境:測試
WSL + PHP 7.2.12code
而後跑下面的代碼:
<?php define('TEST_COUNT', 10000); function test($name, $callable) { $time = microtime(true); $callable(); echo $name, ' time: ', microtime(true) - $time, 's', PHP_EOL; } function myTest($name, $data) { echo $name, ':', PHP_EOL; $encodeResult = json_encode($data); echo 'json_encode size: ', strlen($encodeResult), PHP_EOL; test('json_encode', function() use($data){ for($i = 0; $i < TEST_COUNT; ++$i) { json_encode($data); } }); test('json_decode', function() use($encodeResult){ for($i = 0; $i < TEST_COUNT; ++$i) { json_encode($encodeResult); } }); $encodeResult = serialize($data); echo 'serialize size: ', strlen($encodeResult), PHP_EOL; test('serialize', function() use($data){ for($i = 0; $i < TEST_COUNT; ++$i) { serialize($data); } }); test('unserialize', function() use($encodeResult){ for($i = 0; $i < TEST_COUNT; ++$i) { unserialize($encodeResult); } }); echo '----------------', PHP_EOL; } function test1() { $data = [ 'id' => 1, 'name' => '宇潤', ]; myTest(__FUNCTION__, $data); } function test2() { $data = [ [ 'id' => 1, 'name' => '宇潤', ], [ 'id' => 2, 'name' => '路人', ], [ 'id' => 3, 'name' => '老王', ], [ 'id' => 4, 'name' => '烏龜', ], [ 'id' => 5, 'name' => '甲魚', ], ]; myTest(__FUNCTION__, $data); } function test3() { $data = new stdClass; $data->name = 'testName'; $data->age = 250; $data->hash = md5(250); myTest(__FUNCTION__, $data); } function test4() { $data = []; for($i = 0; $i < 10; ++$i) { $obj = new stdClass; $obj->name = 'testName' . $i; $obj->age = $i; $obj->hash = md5($i); $data[] = $obj; } myTest(__FUNCTION__, $data); } function test5() { $data = json_decode(file_get_contents(__DIR__ . '/data.json')); myTest(__FUNCTION__, $data); } function test6() { $data = json_decode(file_get_contents(__DIR__ . '/data.json'), true); myTest(__FUNCTION__, $data); } test1(); test2(); test3(); test4(); test5(); test6();
結果:
test1: json_encode size: 30 json_encode time: 0.0017490386962891s json_decode time: 0.0014638900756836s serialize size: 43 serialize time: 0.0014791488647461s unserialize time: 0.0049228668212891s ---------------- test2: json_encode size: 156 json_encode time: 0.0059618949890137s json_decode time: 0.0056710243225098s serialize size: 241 serialize time: 0.0055370330810547s unserialize time: 0.019223928451538s ---------------- test3: json_encode size: 71 json_encode time: 0.0030298233032227s json_decode time: 0.0024290084838867s serialize size: 112 serialize time: 0.0026299953460693s unserialize time: 0.010625839233398s ---------------- test4: json_encode size: 711 json_encode time: 0.025734186172485s json_decode time: 0.024907827377319s serialize size: 1157 serialize time: 0.022525072097778s unserialize time: 0.083372831344604s ---------------- test5: json_encode size: 63741 json_encode time: 1.7440521717072s json_decode time: 1.8029508590698s serialize size: 64325 serialize time: 0.82009410858154s unserialize time: 2.8286778926849s ---------------- test6: json_encode size: 63741 json_encode time: 1.7795789241791s json_decode time: 1.7996029853821s serialize size: 62193 serialize time: 0.69928193092346s unserialize time: 1.822273015976s ----------------
JSON
在體積和性能上更爲佔優點serialize
性能更好,體積也稍小unserialize
老是比 serialize
成噸地慢JSON
仍是比較穩的,若是沒有恢復對象狀態需求的話結論僅供參考,測試的數據種類仍是太少,請結合自身場景來測試和選擇