<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Computer, Electron and Technology &#187; 数组</title>
	<atom:link href="http://www.donevii.com/post/tag/%e6%95%b0%e7%bb%84/feed" rel="self" type="application/rss+xml" />
	<link>http://www.donevii.com</link>
	<description>DoneVII CET &#38; CPPLITE</description>
	<lastBuildDate>Wed, 02 Jun 2010 10:45:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>[php5]静态类比全局数组快</title>
		<link>http://www.donevii.com/post/338.html</link>
		<comments>http://www.donevii.com/post/338.html#comments</comments>
		<pubDate>Wed, 04 Jul 2007 03:31:08 +0000</pubDate>
		<dc:creator>gavinkwoe</dc:creator>
				<category><![CDATA[web]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[全局]]></category>
		<category><![CDATA[数组]]></category>
		<category><![CDATA[测试]]></category>
		<category><![CDATA[类]]></category>
		<category><![CDATA[静态]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=338</guid>
		<description><![CDATA[好吧，我承认，标题是有点误导人了&#8230;&#8230; 第一种测试方法： &#60;?php/**&#160;* @date&#160;:&#160;Wed Jul 04 11:34:00 CST 2007 11:34:00&#160;* @author&#160;:&#160;Deng Wei &#60;dengwei@gmail.com&#62;&#160;* @package&#160;:&#16... ]]></description>
			<content:encoded><![CDATA[<p>好吧，我承认，标题是有点误导人了&hellip;&hellip;</p>
<p>第一种测试方法：</p>
<p>&lt;?<a href="http://www.donevii.com/post/tag/php" class="st_tag internal_tag" rel="tag" title="Posts tagged with php">php</a><br />/**<br />&nbsp;* @date&nbsp;:&nbsp;Wed Jul 04 11:34:00 CST 2007 11:34:00<br />&nbsp;* @author&nbsp;:&nbsp;Deng Wei &lt;<a href="mailto:dengwei@gmail.com">dengwei@gmail.com</a>&gt;<br />&nbsp;* @package&nbsp;:&nbsp;test<br />&nbsp;* @version&nbsp;:&nbsp;$Rev$ <br />&nbsp;* @info&nbsp;:&nbsp;$Id$<br />&nbsp;*/</p>
<p>$sth_like_config = array(<br />&nbsp;&#8217;a'&nbsp;=&gt;&nbsp;1,<br />&nbsp;&#8217;b'&nbsp;=&gt;&nbsp;2,<br />);</p>
<p>function get_config_a()<br />{<br />&nbsp;global $sth_like_config;<br />&nbsp;foreach ( range(1,10000) as $i )<br />&nbsp;{<br />&nbsp;&nbsp;$b = $sth_like_config['a'];<br />&nbsp;}<br />}</p>
<p><a href="http://www.donevii.com/post/tag/class" class="st_tag internal_tag" rel="tag" title="Posts tagged with class">class</a> StaticConfig<br />{<br />&nbsp;public static $sth_like_config = array(<br />&nbsp;&nbsp;&#8217;a'&nbsp;=&gt;&nbsp;1,<br />&nbsp;&nbsp;&#8217;b'&nbsp;=&gt;&nbsp;2,<br />&nbsp;);<br />}</p>
<p>function microtime_float()<br />{<br />&nbsp;&nbsp; list($usec, $sec) = explode(&quot; &quot;, microtime());<br />&nbsp;&nbsp; return ((float)$usec + (float)$sec);<br />}</p>
<p>/**<br />&nbsp;* start test<br />&nbsp;*/</p>
<p>$s = microtime_float();<br />get_config_a();<br />echo microtime_float()-$s;<br />echo &quot;\n&quot;;</p>
<p>$s = microtime_float();<br />foreach ( range(1,10000) as $i )<br />{<br />&nbsp;$b = StaticConfig::$sth_like_config['a'];<br />}<br />echo microtime_float()-$s;<br />echo &quot;\n&quot;;</p>
<p>?&gt;</p>
<p>结果：</p>
<p>0.15xxxxxxxx<br />0.18xxxxxxxxx</p>
<p>按上面这种方式测试的话是数组比静态类快，原因是我们在函数里调用的全局数组，只用了 global 一次，在同一作用域下变量比类快，这是一定的。但是在实际环境里，比如我们的配置数组经常要在一些函数里用，那么就会有多次 global 声明，让我们换一种方式测试。</p>
<p>测试方法二：</p>
<p>&lt;?<a href="http://www.donevii.com/post/tag/php" class="st_tag internal_tag" rel="tag" title="Posts tagged with php">php</a><br />/**<br />&nbsp;* @date&nbsp;:&nbsp;Wed Jul 04 11:34:00 CST 2007 11:34:00<br />&nbsp;* @author&nbsp;:&nbsp;Deng Wei &lt;<a href="mailto:dengwei@gmail.com">dengwei@gmail.com</a>&gt;<br />&nbsp;* @package&nbsp;:&nbsp;test<br />&nbsp;* @version&nbsp;:&nbsp;$Rev$ <br />&nbsp;* @info&nbsp;:&nbsp;$Id$<br />&nbsp;*/</p>
<p>$sth_like_config = array(<br />&nbsp;&#8217;a'&nbsp;=&gt;&nbsp;1,<br />&nbsp;&#8217;b'&nbsp;=&gt;&nbsp;2,<br />);</p>
<p>function get_config_a()<br />{<br />&nbsp;global $sth_like_config;<br />&nbsp;return $sth_like_config['a'];<br />}</p>
<p><a href="http://www.donevii.com/post/tag/class" class="st_tag internal_tag" rel="tag" title="Posts tagged with class">class</a> StaticConfig<br />{<br />&nbsp;public static $sth_like_config = array(<br />&nbsp;&nbsp;&#8217;a'&nbsp;=&gt;&nbsp;1,<br />&nbsp;&nbsp;&#8217;b'&nbsp;=&gt;&nbsp;2,<br />&nbsp;);<br />}</p>
<p>function microtime_float()<br />{<br />&nbsp;&nbsp; list($usec, $sec) = explode(&quot; &quot;, microtime());<br />&nbsp;&nbsp; return ((float)$usec + (float)$sec);<br />}</p>
<p>/**<br />&nbsp;* start test<br />&nbsp;*/</p>
<p>$s = microtime_float();<br />foreach ( range(1,10000) as $i )<br />{<br />&nbsp;$b = get_config_a();<br />}<br />echo microtime_float()-$s;<br />echo &quot;\n&quot;;</p>
<p>$s = microtime_float();<br />foreach ( range(1,10000) as $i )<br />{<br />&nbsp;$b = StaticConfig::$sth_like_config['a'];<br />}<br />echo microtime_float()-$s;<br />echo &quot;\n&quot;;</p>
<p>?&gt;</p>
<p>结果：</p>
<p>0.51952910423279<br />0.18489503860474</p>
<p>可见 global 有多慢了，并且我们经常还会因为一些原因忘记写他，所以我的&ldquo;建议&rdquo;是把一些全局变量都放到静态类里，这样还用的时候就不用一直记着去 global 声明了，并且通过类来访问，我们还可以对访问进行一些控制。：）</p>
<p>不知道您是怎么想？我现在是把配置信息都放到静态类里了。终于告别了&ldquo;global&rdquo;~！</p>

	<h4>相关文章</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.donevii.com/post/260.html" title="英文网站大全 (2006-12-28)">英文网站大全</a> (0)</li>
	<li><a href="http://www.donevii.com/post/110.html" title="翻译：On having layout (2006-10-24)">翻译：On having layout</a> (0)</li>
	<li><a href="http://www.donevii.com/post/640.html" title="二十一世纪让人悲伤的七个原因 (2008-12-01)">二十一世纪让人悲伤的七个原因</a> (0)</li>
	<li><a href="http://www.donevii.com/post/137.html" title="SOAP(简单对象访问协议) 简介 (2006-10-27)">SOAP(简单对象访问协议) 简介</a> (0)</li>
	<li><a href="http://www.donevii.com/post/290.html" title="PHP5 效率优化 (2007-01-13)">PHP5 效率优化</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/338.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
