同時對以下幾種PHP的序列化方案進行了測試:

  • serialize() and unserialize()
  • json_encode() and json_decode()
  • msgpack_serialize() and msgpack_unserialize()
  • igbinary_serialize() and igbinary_unserialize()

測試環境:

  • OS: Archlinux 32-bit
  • CPU: CORE 2 DUO T9400, 2.53GHz
  • Mem: DDR3, 3G
  • Server: nginx v1.0.0
  • PHP: php v5.3.6 + php-fpm
  • Profiler: xhprof v0.9.2

測試代碼塊:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
class Test{
function __construct(){
$this->value = str_repeat('a', 1000);
}
}
function produce($num)
{
$arr = array();
for ($i = 0; $i < $num; $i++) {
$arr[] = new Test();
}
$data = serialize($arr);
$arr = unserialize($data);
$data = json_encode($arr);
$arr = json_decode($data);
$data = msgpack_serialize($arr);
$arr = msgpack_unserialize($data);
$data = igbinary_serialize($arr);
$arr = igbinary_unserialize($data);
}
produce(10000);
?>

測試結果:

  • 序列化性能:serialize() 24.611ms > msgpack_serialize() 32.687ms > igbinary_serialize() 36.012ms > json_encode() 132.142ms
  • 反序列化性能:igbinary_unserialize() 12.141ms > msgpack_unserialize() 17.185ms > unserialize() 28.723ms > json_decode() 183.141ms
  • 綜合性能:igbinary 48.153ms > msgpack 49.872ms > serialize 53.334ms > json 315.283ms

測試結果并沒有像傳說中的那樣,官方的序列化和反序列化函數性能與msgpack和igbinary兩個第三方的擴展相差不大,而JSON性能非常差。

Posted via UltraBlog.vim.