php implode/explode, serialize, json, msgpack 效能對比,implodejson

來源:互聯網
上載者:User

php implode/explode, serialize, json, msgpack 效能對比,implodejson

php implode/explode, serialize, json, msgpack 效能對比


首先使用implode, serialize, json_encode, msgpack_pack建立四個文字檔,用於測試。

建立代碼如下:

<?php$arr = array(    'content1' => '一二三四五六七八九十',    'content2' => '一二三四五六七八九十',    'content3' => '一二三四五六七八九十');echo file_put_contents('implode.txt', implode(',',$arr), true).'<br>';echo file_put_contents('serialize.txt', serialize($arr), true).'<br>';echo file_put_contents('json.txt', json_encode($arr), true).'<br>';echo file_put_contents('msgpack.txt', msgpack_pack($arr), true);?>

建立後產生

implode.txt    92位元組
serialize.txt   165位元組
json.txt          223位元組
msgpack.txt  121位元組


產生的字串大小排序如下 implode < msgpack_pack < serialize < json_encode

如果數組簡單,則json_encode有可能比serialize小

例如:

$arr = array('一','二','三','四','五','六','七','八','九','十');
serialize   為147位元組

json_encode 為91位元組


比較 implode, serialize, json_encode, msgpack_pack 效能
<?php$arr = array(    'content1' => '一二三四五六七八九十',    'content2' => '一二三四五六七八九十',    'content3' => '一二三四五六七八九十');$start = microtime(true);$i = 1000000;while($i>0){    // 分別測試回合時間及記憶體使用量情況    $tmp = implode(',',$arr);    // $tmp = serialize($arr);    // $tmp = json_encode($arr);    // $tmp = msgpack_pack($arr);    $i--;}$end = microtime(true);echo 'run time:'.($end-$start).'s<br>';echo 'memory usage:'.(memory_get_usage()/1024).'KB';?>

implode       1.3225722312927s    628.50KBserialize     2.0553789138794s    628.32KBjson_encode   2.5058920383453s    628.34KBmsgpack_pack  1.6431028842926s    628.24KB

結果:記憶體使用量情況差不多,已耗用時間 implode < msgpack_pack < serialize < json_encode


比較 explode, unserialize, json_decode, msgpack_unpack 效能

<?php$data = file_get_contents('implode.txt');//$data = file_get_contents('serialize.txt');//$data = file_get_contents('json.txt');//$data = file_get_contents('msgpack.txt');$start = microtime(true);$i = 1000000;while($i>0){    $tmp = explode(',',$data);    //$tmp = unserialize($data);    //$tmp = json_decode($data, true);    //$tmp = msgpack_unpack($data);    $i--;}$end = microtime(true);echo 'run time:'.($end-$start).'s<br>';echo 'memory usage:'.(memory_get_usage()/1024).'KB';?>

explode         1.7446749210358s    628.74KBunserialize     2.1386790275574s    628.67KBjson_decode     5.2423169612885s    628.84KBmsgpack_unpack  2.2290098667145s    628.63KB

結果:記憶體使用量情況差不多,已耗用時間 explode < serialize < msgpack_unpack < json_decode


總結,由於implode/explode不適合使用複雜的結構,因此常用的為serialize,json,msgpack三種。

而三種比較,運行速度,記憶體佔用,空間佔用最優為msgpack, 其次是serialize,最後是json。

如有條件,建議使用msgpack序列化處理資料。


關於msgpack 可以查看我之前寫的文章:《MessagePack 序列化格式》


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.