php 数组存储简单比较(json/serialize/igbi
2018-08-29 本文已影响109人
零一间
环境
image.pngimage.png
简单测试代码
<?php
/**
* 测试函数
* @param $array
*/
function run($array){
//JSON
$start = microtime(true);
$export = json_encode($array);
$end = microtime(true);
$duration = $end - $start;
print('JSON Encode: ' . $duration . PHP_EOL);
$start = microtime(true);
$import = json_decode($export);
$end = microtime(true);
$duration = $end - $start;
print('JSON Decode: ' . $duration . PHP_EOL);
//serialize
$start = microtime(true);
$export = serialize($array);
$end = microtime(true);
$duration = $end - $start;
print('Serialize: ' . $duration . PHP_EOL);
$start = microtime(true);
$import = unserialize($export);
$end = microtime(true);
$duration = $end - $start;
print('Unserialize: ' . $duration . PHP_EOL);
//igbinary_serialize
$start = microtime(true);
$export = igbinary_serialize($array);
$end = microtime(true);
$duration = $end - $start;
print('Igbinary Serialize: ' . $duration . PHP_EOL);
$start = microtime(true);
$import = igbinary_unserialize($export);
$end = microtime(true);
$duration = $end - $start;
print('Igbinary UnSerialize: ' . $duration . PHP_EOL);
//hprose_serialize
require_once './vendor/autoload.php';
$start = microtime(true);
$export = hproseformatter::serialize($array);
$end = microtime(true);
$duration = $end - $start;
print('Hprose Serialize: ' . $duration . PHP_EOL);
$start = microtime(true);
$import = hproseformatter::unserialize($export);
$end = microtime(true);
$duration = $end - $start;
print('Hprose Unserialize: ' . $duration . PHP_EOL);
}
测试结果
10000个数组元素
ini_set('memory_limit', '2048m');
$array = array_fill(0, 10000, rand(1, 9999));
run($array);
输出结果
JSON Encode: 0
JSON Decode: 0.003000020980835
Serialize: 0.0030009746551514
Unserialize: 0.0030009746551514
Igbinary Serialize: 0.0010001659393311
Igbinary UnSerialize: 0.00099992752075195
Hprose Serialize: 0.34606909751892
Hprose Unserialize: 0.34206795692444
5000000个数组元素
ini_set('memory_limit', '2048m');
$array = array_fill(0, 5000000, rand(1, 9999));
run($array);
输出结果
JSON Encode: 0.96700000762939
JSON Decode: 1.6029999256134
Serialize: 10.430000066757
Unserialize: 10.762000083923
Igbinary Serialize: 1.9860000610352
Igbinary UnSerialize: 0.8510000705719
Hprose Serialize: 138.22799992561
Hprose Unserialize: 170.71580314636
测试结果仅供参考(影响结果的因素有很多,~_~),一般不会遇到如此极端情况!