同时对以下几种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.