我需要在平面文件中存储多维数据关联数组,以便进行缓存。我可能偶尔会遇到需要将其转换为JSON以在我的web应用程序中使用,但绝大多数情况下,我将直接在PHP中使用数组。

在这个文本文件中将数组存储为JSON还是PHP序列化数组更有效?我查看了一下,似乎在最新版本的PHP(5.3)中,json_decode实际上比反序列化更快。

我目前倾向于将数组存储为JSON,因为我觉得如果有必要的话,它更容易被人阅读,它可以在PHP和JavaScript中使用,而且从我所读到的,它甚至可能更快地解码(虽然不确定编码)。

有人知道有什么陷阱吗?有人有好的基准来显示这两种方法的性能优势吗?


当前回答

我已经在一个相当复杂、嵌套简单、包含各种数据(字符串、NULL、整数)的多散列上对此进行了非常彻底的测试,序列化/反序列化最终比json_encode/json_decode快得多。

在我的测试中,json的唯一优势是它的“打包”大小更小。

这些都是在PHP 5.3.3下完成的,如果你想了解更多细节,请告诉我。

下面是测试结果,然后是生成它们的代码。我不能提供测试数据,因为它会泄露一些我不能公开的信息。

JSON encoded in 2.23700618744 seconds
PHP serialized in 1.3434419632 seconds
JSON decoded in 4.0405561924 seconds
PHP unserialized in 1.39393305779 seconds

serialized size : 14549
json_encode size : 11520
serialize() was roughly 66.51% faster than json_encode()
unserialize() was roughly 189.87% faster than json_decode()
json_encode() string was roughly 26.29% smaller than serialize()

//  Time json encoding
$start = microtime( true );
for($i = 0; $i < 10000; $i++) {
    json_encode( $test );
}
$jsonTime = microtime( true ) - $start;
echo "JSON encoded in $jsonTime seconds<br>";

//  Time serialization
$start = microtime( true );
for($i = 0; $i < 10000; $i++) {
    serialize( $test );
}
$serializeTime = microtime( true ) - $start;
echo "PHP serialized in $serializeTime seconds<br>";

//  Time json decoding
$test2 = json_encode( $test );
$start = microtime( true );
for($i = 0; $i < 10000; $i++) {
    json_decode( $test2 );
}
$jsonDecodeTime = microtime( true ) - $start;
echo "JSON decoded in $jsonDecodeTime seconds<br>";

//  Time deserialization
$test2 = serialize( $test );
$start = microtime( true );
for($i = 0; $i < 10000; $i++) {
    unserialize( $test2 );
}
$unserializeTime = microtime( true ) - $start;
echo "PHP unserialized in $unserializeTime seconds<br>";

$jsonSize = strlen(json_encode( $test ));
$phpSize = strlen(serialize( $test ));

echo "<p>serialized size : " . strlen(serialize( $test )) . "<br>";
echo "json_encode size : " . strlen(json_encode( $test )) . "<br></p>";

//  Compare them
if ( $jsonTime < $serializeTime )
{
    echo "json_encode() was roughly " . number_format( ($serializeTime / $jsonTime - 1 ) * 100, 2 ) . "% faster than serialize()";
}
else if ( $serializeTime < $jsonTime )
{
    echo "serialize() was roughly " . number_format( ($jsonTime / $serializeTime - 1 ) * 100, 2 ) . "% faster than json_encode()";
} else {
    echo 'Unpossible!';
}
    echo '<BR>';

//  Compare them
if ( $jsonDecodeTime < $unserializeTime )
{
    echo "json_decode() was roughly " . number_format( ($unserializeTime / $jsonDecodeTime - 1 ) * 100, 2 ) . "% faster than unserialize()";
}
else if ( $unserializeTime < $jsonDecodeTime )
{
    echo "unserialize() was roughly " . number_format( ($jsonDecodeTime / $unserializeTime - 1 ) * 100, 2 ) . "% faster than json_decode()";
} else {
    echo 'Unpossible!';
}
    echo '<BR>';
//  Compare them
if ( $jsonSize < $phpSize )
{
    echo "json_encode() string was roughly " . number_format( ($phpSize / $jsonSize - 1 ) * 100, 2 ) . "% smaller than serialize()";
}
else if ( $phpSize < $jsonSize )
{
    echo "serialize() string was roughly " . number_format( ($jsonSize / $phpSize - 1 ) * 100, 2 ) . "% smaller than json_encode()";
} else {
    echo 'Unpossible!';
}

其他回答

仅供参考——如果您想将数据序列化为易于阅读和理解的JSON,但具有更多的压缩和更高的性能,您应该检查messagpack。

如果您正在缓存的信息最终希望在稍后的时间点“包含”,那么您可能希望尝试使用var_export。这样你只在“序列化”中受到打击,而不是在“反序列化”中受到打击。

我也做了一个小的基准测试。结果是一样的。但是我需要解码性能。我注意到,就像上面几个人说的,unserialize比json_decode快。反序列化大约占用json_decode时间的60-70%。所以结论很简单: 当需要编码时的性能时,使用json_encode,当需要解码时的性能时,使用unserialize。因为你不能合并这两个功能,你必须做出选择,你需要更多的性能。

我在pseudo中的基准:

用一些随机键和值定义数组$arr 对于x < 100;x + +;序列化和json_encode $arr的array_rand 对于y < 1000;y + +;Json_decode json编码的string - calc时间 对于y < 1000;y + +;取消序列化的string - calc时间 回声更快的结果

平均而言:unserialize赢得了96次胜过json_decode的4次。平均约1.5ms超过2.5ms。

THX -用于此基准代码:

我的结果对阵列我使用的配置是休耕: JSON编码在0.0031511783599854秒 PHP在0.0037961006164551秒内序列化 Json_encode()比serialize()快20.47% JSON编码在0.0070841312408447秒 PHP在0.0035839080810547秒内序列化 Unserialize()比json_encode()快97.66%

用你自己的数据进行测试。

您可能还会对https://github.com/phadej/igbinary感兴趣——它为PHP提供了一个不同的序列化“引擎”。

我的随机/任意的“性能”数据,使用PHP 5.3.5在64位平台上显示:

JSON:

JSON编码在2.180496931076秒 JSON解码在9.8368630409241秒 serialized "String" size: 13993

原生PHP:

PHP在2.9125759601593秒内序列化 PHP在6.4348418712616秒内反序列化 序列化的“字符串”大小:20769

Igbinary:

winigbinary在1.6099879741669秒内序列化 winigbinary在4.7737920284271秒内未序列化 WIN序列化的“字符串”大小:4467

因此,igbinary_serialize()和igbinary_unserialize()更快,使用更少的磁盘空间。

我使用fillArray(0,3)代码,但使数组键更长的字符串。

igbinary可以存储与PHP原生序列化相同的数据类型(所以对象等没有问题),如果你愿意,你可以告诉PHP5.3使用它来进行会话处理。

参见http://ilia.ws/files/zendcon_2010_hidden_features.pdf -特别是幻灯片14/15/16