简体   繁体   English

哪个解析速度更快? 序列化的字符串或纯PHP还是其他?

[英]Which is faster to parse? Serialized string or plain PHP or something else?

So, for performance reasons, I need my app to store big arrays of data in a way that's fast to parse. 因此,出于性能原因,我需要我的应用以快速解析的方式存储大量数据。 I know JSON is readable but it's not fast to decode. 我知道JSON可读,但解码速度不快。 So it's I should either convert my array into pure php code or I have to serialize it and then deserialize. 因此,我应该将数组转换为纯PHP代码,或者必须对其进行序列化然后反序列化。 So, which is faster? 那么,哪个更快? Are there any better solutions? 有更好的解决方案吗? I could do a benchmark myself, but It's always better to consider other people's experiences :) 我可以自己做一个基准测试,但是考虑其他人的经验总是更好的:)

More info: By big array I mean something with about 2MB worth of data returned from calling print_r() on it! 更多信息:大数组是指通过调用print_r()返回大约2MB的数据! and by converting it into pure php code I mean this: suppose this is my array: {"index1":"value1","index2":"val'ue2"} and this would what the hypothetical function convert_array_to_php() would return: 并将其转换成纯PHP代码,我的意思是:假设这是我的数组: {"index1":"value1","index2":"val'ue2"} ,假设的功能convert_array_to_php()将返回以下内容:

$array = array('index1'=>'value1' ,'index2'=>'val\'ue2');

Depends on the data and usage patterns. 取决于数据和使用模式。

Generally unserialize() is faster than json_decode(), which is faster than include(). 通常,unserialize()比json_decode()快,而json_decode()比include()快。 However with large data amounts, the bottleneck is actually the disk. 但是,数据量大时,瓶颈实际上是磁盘。 So unserialize(gzdecode(file_get_contents())) is often the fastest. 因此, unserialize(gzdecode(file_get_contents()))通常是最快的。 The difference in decoding speed might be neglectible in comparison to reading it from disk. 与从磁盘读取相比,解码速度的差异可以忽略不计。

If you don't really need to read out the complete data set for printing or calculation, then the fastest storage might be SQLite however. 如果您真的不需要读取完整的数据集以进行打印或计算,则最快的存储可能是SQLite。 It often keeps indexes in memory. 它通常将索引保留在内存中。

Well, I did a little benchmark, I put about 7MB of pure php coded array into a php file, and also put it's json version in another file and also a serialized version. 好吧,我做了一个基准测试,将大约7MB的纯php编码数组放入一个php文件中,并将它的json版本放入另一个文件中,以及一个序列化版本中。 Then did a benchmark on all three of them and here is the result: As expected, the json format was the slowest to decode, it took about 3 times longer than the pure php code to parse. 然后对所有这三个参数进行了基准测试,结果如下:正如预期的那样,json格式是最慢的解码方式,它花了比纯php代码解析时间大约三倍的时间。 And it's interesting to know that unserialize() was the fastest one, performing around 4 times faster than the native php code. 有趣的是,unserialize()是最快的,其执行速度比本地php代码快约4倍。

Pure php code will probably have to be the fastest . 纯PHP代码可能必须是最快的 However, it's unlikely to be the best option, because it is probably harder to maintain. 但是,它不太可能是最佳选择,因为它可能更难维护。 It depends on the nature of the data though. 但是,这取决于数据的性质。

Isn't there a better option available but relying on PHP solely for this to? 没有更好的选择,而是仅依靠PHP来做到这一点吗?

I am guessing that handling a few arrays of this size is going to hit your server quite hard. 我猜想处理这种大小的几个阵列将对您的服务器造成很大的影响。
Is it possible for you to maybe utilize a database with some temporary tables to do what you need to do with the data in the arrays? 您是否可能利用带有一些临时表的数据库来处理数组中的数据?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM