在PHP中序列化一个大数组? [英] serialize a large array in PHP?
问题描述
我很好奇,在PHP中序列化是否有大小限制?可以对具有5,000个键和值的数组进行序列化,以便将其存储到缓存中吗?
I am curious, is there a size limit on serialize in PHP. Would it be possible to serialize an array with 5,000 keys and values so it can be stored into a cache?
我希望在社交网站上缓存用户的朋友列表,缓存需要进行相当频繁的更新,但是几乎每次加载页面时都需要读取缓存.
I am hoping to cache a users friend list on a social network site, the cache will need to be updated fairly often but it will need to be read almost every page load.
在单个服务器上,我假设APC会比内存缓存更好.
On a single server setup I am assuming APC would be better then memcache for this.
推荐答案
很多人已经回答了,只是为了好玩,这是一个非常快速的基准测试(我敢称呼它吗?);考虑以下代码:
As quite a couple other people answered already, just for fun, here's a very quick benchmark (do I dare calling it that ? ) ; consider the following code :
$num = 1;
$list = array_fill(0, 5000, str_repeat('1234567890', $num));
$before = microtime(true);
for ($i=0 ; $i<10000 ; $i++) {
$str = serialize($list);
}
$after = microtime(true);
var_dump($after-$before);
var_dump(memory_get_peak_usage());
我正在PHP 5.2.6(与Ubuntu jaunty捆绑在一起的版本)上运行此代码.
而且,是的,只有值;没有钥匙;值非常简单:没有对象,没有子数组,只有字符串.
对于$num = 1
,您会得到:
float(11.8147978783)
int(1702688)
对于$num = 10
,您会得到:
float(13.1230671406)
int(2612104)
然后,对于$num = 100
,您将获得:
And, for $num = 100
, you get :
float(63.2925770283)
int(11621760)
因此,似乎数组中的每个元素越大,所需的时间就越长.但是,对于大100倍的元素,您所花费的时间不会长100倍...
So, it seems the bigger each element of the array is, the longer it takes (seems fair, actually). But, for elements 100 times bigger, you don't take 100 times much longer...
现在,使用50000个元素的数组,而不是5000个元素,这意味着这部分代码已更改:
Now, with an array of 50000 elements, instead of 5000, which means this part of the code is changed :
$list = array_fill(0, 50000, str_repeat('1234567890', $num));
使用$num = 1
,您将获得:
float(158.236332178)
int(15750752)
考虑到花费1的时间,我将不会同时为$ num = 10或$ num = 100 ...
Considering the time it took for 1, I won't be running this for either $num = 10 nor $num = 100...
是的,当然,在实际情况下,您不会这样做10000次;因此,让我们尝试使用for循环的10次迭代.
Yes, of course, in a real situation, you wouldn't be doing this 10000 times ; so let's try with only 10 iterations of the for loop.
对于$num = 1
:
float(0.206310987473)
int(15750752)
对于$num = 10
:
float(0.272629022598)
int(24849832)
对于$num = 100
:
float(0.895547151566)
int(114949792)
是的,这差不多是1秒-占用了相当多的内存^^
(不,这不是生产服务器:我在此开发机上有一个相当高的memory_limit ^^)
Yeah, that's almost 1 second -- and quite a bit of memory used ^^
(No, this is not a production server : I have a pretty high memory_limit on this development machine ^^ )
因此,最后要比那些数字短一些-是的,您可以让数字说出您想要的数字-我不会说有一个极限"就像PHP中的硬编码"一样,但是您最终将面临其中之一:
So, in the end, to be a bit shorter than those number -- and, yes, you can have numbers say whatever you want them to -- I wouldn't say there is a "limit" as in "hardcoded" in PHP, but you'll end up facing one of those :
-
max_execution_time
(通常在网络服务器上,时间不会超过30秒) -
memory_limit
(在网络服务器上,muco通常不会超过32MB) - 您的Web服务器将承受的负载:当那些大的serialize-loop之一正在运行时,它占用了我的CPU之一;如果您同时在同一页面上有多个用户,那么我想您会想象这将给您带来什么;-)
- 您的用户的耐心^^
max_execution_time
(generally, on a webserver, it's never more than 30 seconds)memory_limit
(on a webserver, it's generally not muco more than 32MB)- the load you webserver will have : while 1 of those big serialize-loop was running, it took 1 of my CPU ; if you are having quite a couple of users on the same page at the same time, I let you imagine what it will give ;-)
- the patience of your user ^^
但是,除非您真的要序列化大数据长数组,否则我不确定这有多重要...
并且您必须考虑使用该缓存的时间/CPU负载量可能会帮助您获得;-)
But, except if you are really serializing long arrays of big data, I am not sure it will matter that much...
And you must take into consideration the amount of time/CPU-load using that cache might help you gain ;-)
仍然,最好的了解方法是使用真实数据自己进行测试;-)
Still, the best way to know would be to test by yourself, with real data ;-)
您可能还想看看 Xdebug 在
And you might also want to take a look at what Xdebug can do when it comes to profiling : this kind of situation is one of those it is useful for!
这篇关于在PHP中序列化一个大数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!