<?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>脚本 &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/%E8%84%9A%E6%9C%AC/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Mon, 08 Oct 2012 08:33:17 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.1</generator>
	<item>
		<title>PHP计算页面执行时间函数</title>
		<link>https://www.mudbest.com/php%e8%ae%a1%e7%ae%97%e9%a1%b5%e9%9d%a2%e6%89%a7%e8%a1%8c%e6%97%b6%e9%97%b4%e5%87%bd%e6%95%b0/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Sun, 07 Oct 2012 06:11:47 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[优化]]></category>
		<category><![CDATA[微妙]]></category>
		<category><![CDATA[执行时间]]></category>
		<category><![CDATA[脚本]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1542</guid>

					<description><![CDATA[很多时候想要知道脚本效率时间，这时候就需要知道脚本的运行时间，然后去优化它。可能有些人看到了Discuz页面底 ... <a title="PHP计算页面执行时间函数" class="read-more" href="https://www.mudbest.com/php%e8%ae%a1%e7%ae%97%e9%a1%b5%e9%9d%a2%e6%89%a7%e8%a1%8c%e6%97%b6%e9%97%b4%e5%87%bd%e6%95%b0/" aria-label="阅读 PHP计算页面执行时间函数">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>很多时候想要知道脚本效率时间，这时候就需要知道脚本的运行时间，然后去优化它。可能有些人看到了Discuz页面底部也会有一个“Processed in 0.030282 second(s)&#8230;”这样的提示，对，这就是脚本执行时间。</p>
<p>如何计算一段php程序代码的执行消耗时间？</p>
<p>对于系统时间,可能很多programmer对php的time()函数并不陌生,可惜time()函数只返回,自从 Unix 纪元（格林威治时间 1970 年 1 月 1 日 00:00:00）到当前时间的秒数。<br />
没错,是秒.我们一段php程序代码执行耗时可能并不超过一秒,所以time()并不适用。<span id="more-1542"></span></p>
<p>php提供了一个更为精确的时间函数microtime():</p>
<p>microtime — 返回当前 Unix 时间戳和微秒数。</p>
<p>格式: mixed microtime ([ bool $get_as_float ] )</p>
<p>函数以 “msec sec” 的格式返回一个字符串,sec 是自 Unix 纪元（0:00:00 January 1, 1970 GMT）起到现在的秒数，msec 是微秒部分。<br />
如果输入参数为true,microtime() 将返回一个浮点数。</p>
<p>好了，然后的事情很简单，为了免于对返回的结果做复杂的字符串转换，我们设定microtime()输入参数为true，使得其返回的结果为浮点数。<br />
然后在程序开始和结束分别计算一次，两次结果相减便是程序执行的时间。(PHP手册里有一句话：永远不要比较两个浮点数是否相等。 )<br />
最后使用number_format格式化浮点数。此方法仅供测试，结果不一定精确。</p>
<p>Example #1 number_format() Example：</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
$number = 1234.56;

// english notation (default)
$english_format_number = number_format($number);
// 1,235

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?&gt;
</pre>
<p>Example #1 用 microtime() 对脚本的运行计时</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
/**
 * Simple function to replicate PHP 5 behaviour
 */
function microtime_float() {
	list ( $usec, $sec ) = explode ( &quot; &quot;, microtime () );
	return (( float ) $usec + ( float ) $sec);
}

$time_start = microtime_float ();

// Sleep for a while
usleep ( 100 );

$time_end = microtime_float ();
$time = $time_end - $time_start;

echo &quot;Did nothing in $time seconds\n&quot;;
?&gt;
</pre>
<p>类：</p>
<pre class="brush: php; title: ; notranslate">
&lt;?
/**
 * 页面执行时间计算
 * @author hkshadow
 */
class RunTime//页面执行时间类
{
	private $starttime;//页面开始执行时间
	private $stoptime;//页面结束执行时间
	private $spendtime;//页面执行花费时间
	function getmicrotime()//获取返回当前微秒数的浮点数
	{
		list($usec,$sec)=explode(&quot; &quot;,microtime());
		return ((float)$usec + (float)$sec);
	}
	function start()//页面开始执行函数，返回开始页面执行的时间
	{
		$this-&gt;starttime=$this-&gt;getmicrotime();
	}
	function end()//显示页面执行的时间
	{
		$this-&gt;stoptime=$this-&gt;getmicrotime();
		$this-&gt;spendtime= $this-&gt;stoptime - $this-&gt;starttime;
	}
	function display()
	{
		$this-&gt;end();
		echo &quot;&lt;p&gt;运行时间：&quot;.number_format($this-&gt;spendtime,10).&quot;秒&lt;/p&gt;&quot;;
	}
}

$timer=new Runtime();
$timer-&gt;start();  //执行脚本开始时间 
$timer-&gt;display(); //显示页面(脚本执行结束时间)
?&gt;
</pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
