<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[直接返回数组的方法是最慢的]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[代码编程]]></category>
<pubDate>Sun, 18 Jan 2009 04:08:17 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	缓存文件,大家用的最多的估计就是 return array();<br/><br/>实验证明,这种方法是较慢的,试考查以下的3个文件.<br/><br/>其一. $arr.php 直接返回 数组<br/><br/>&lt;?php<br/>return Array<br/>(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host1&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host2&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host3&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host4&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host5&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host6&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host7&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host8&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host9&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host10&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host11&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host12&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host13&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host14&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host15&quot; =&gt; 123,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;db_host16&quot; =&gt; 123<br/>);<br/>?&gt;<br/><br/>其二,&nbsp;&nbsp;ini.php 为INI文件<br/><br/>&lt;?php exit;?&gt;<br/>db_host1 =123<br/>db_host2 =123<br/>db_host3 =123<br/>db_host4 =123<br/>db_host5 =123<br/>db_host6 =123<br/>db_host7 =123<br/>db_host8 =123 <br/>db_host9 =123<br/>db_host10 =123<br/>db_host11 =123<br/>db_host12 =123<br/>db_host13 =123<br/>db_host14 =123<br/>db_host15 =123<br/>db_host16 =123<br/>其三,str.php 为序例化后的数组<br/><br/>&lt;?php<br/>return <br/>&#039;a:16:{s:8:&quot;db_host1&quot;;s:3:&quot;123&quot;;s:8:&quot;db_host2&quot;;s:3:&quot;123&quot;;s:8:&quot;db_host3&quot;;s:3:&quot;123&quot;;s:8:&quot;db_host4&quot;;s:3:&quot;123&quot;;s:8:&quot;db_host5&quot;;s:3:&quot;123&quot;;s:8:&quot;db_host6&quot;;s:3:&quot;123&quot;;s:8:&quot;db_host7&quot;;s:3:&quot;123&quot;;s:8:&quot;db_host8&quot;;s:3:&quot;123&quot;;s:8:&quot;db_host9&quot;;s:3:&quot;123&quot;;s:9:&quot;db_host10&quot;;s:3:&quot;123&quot;;s:9:&quot;db_host11&quot;;s:3:&quot;123&quot;;s:9:&quot;db_host12&quot;;s:3:&quot;123&quot;;s:9:&quot;db_host13&quot;;s:3:&quot;123&quot;;s:9:&quot;db_host14&quot;;s:3:&quot;123&quot;;s:9:&quot;db_host15&quot;;s:3:&quot;123&quot;;s:9:&quot;db_host16&quot;;s:3:&quot;123&quot;;}&#039;;<br/>?&gt;<br/><br/>分别对3个文件&nbsp;&nbsp;进行10000次包含返回数组测试<br/><br/>$t1 = microtime(true);<br/><br/>$file1 = &#039;./arr.php&#039;;<br/>$file2 = &#039;./ini.php&#039;;<br/>$file3 = &#039;./str.php&#039;;<br/><br/>for($i=0; $i&lt;10000; $i++){<br/>//$arr&nbsp;&nbsp;= require $file1;<br/>//$arr&nbsp;&nbsp;= parse_ini_file($file2);<br/>$arr&nbsp;&nbsp;= unserialize(require $file3);<br/>}<br/><br/>$t2 = microtime(true);<br/>echo $t2-$t1;<br/><br/>得出结果:<br/><br/>arr.php&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5.7820551395416<br/><br/>ini.php&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5.3364160060883<br/><br/>str.php&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5.5691919326782<br/><br/>其中,排得最快的居然是 INI文件.<br/><br/>其次是序列化, 最慢的是直接 return 数组<br/><br/>结论:<br/><br/>&nbsp;&nbsp; ini 文件不错,方便书写(可以考虑)<br/><br/>&nbsp;&nbsp; 保存数组, 用序列化!<br/><br/>Tags - <a href="http://www.dzhope.com/tags/php/" rel="tag">php</a> , <a href="http://www.dzhope.com/tags/%25E7%25BC%2593%25E5%25AD%2598%25E6%2596%2587%25E4%25BB%25B6/" rel="tag">缓存文件</a>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] 直接返回数组的方法是最慢的]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>