PHP序列化方式对比

< ?php
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . DIRECTORY_SEPARATOR . 'Classes');
include 'PHPExcel.php';

$file = __DIR__ . DIRECTORY_SEPARATOR . 'data.xlsx';//约5万行记录
$bm = memory_get_usage(true)/1024/1024;
$objPHPExcel = PHPExcel_IOFactory::load($file);
$em = memory_get_usage(true)/1024/1024;
echo sprintf("%.9f\r\n", $em-$bm);

echo "serialize ...\r\n";
/* 超出内存限制
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . 'data.db', serialize($objPHPExcel));
*/

/* 不能序列化 PHPExcel 对象?
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . 'data.db', json_encode($objPHPExcel));
*/

/* 超出内存限制 It's like JSON. but fast and small.
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . 'data.db', msgpack_pack($objPHPExcel));
*/

/* It's ok. Very fast. */
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . 'data.db', igbinary_serialize($objPHPExcel));
echo "done.";

比 define 和 apc_define_constants 效率更高的定义常量的方式:hidef