2014-03-26 99 views
0

ive做了一個搜索這裏和谷歌,但無法找到任何類似的東西,有沒有辦法看到一個特定的過程需要多長時間在PHP?如何計算使用時間在php

即時運行zend_lucene,並希望輸出多久纔會顯示給用戶的搜索結果。

+0

向右邊看對於如何做到這一點多個示例 - > –

+0

感謝,簡單有效的方法! – Ricky

回答

1

$ ELAPSED_TIME:將包含腳本的執行時間,以秒

$time_start = time(); 

//your code 

$time_end = time(); 

$time_elapsed = $time_end - $time_start; 
+1

建議在代碼中添加一些解釋 - 它做了什麼,它是如何工作的等等。在你的情況下,最後描述'$ time_elapsed'將包含什麼是有用的。 –

+0

我正在編輯答案 – WhiteLine

+0

執行時間不會是unix時間戳 - 它將以秒爲單位。 '$ start_time'和'$ end_time'將是unix時間戳,這是自01/01/1970以來的秒數。當你從另一個減去一個,你會得到秒數。 –

5

我建議這樣的:

microtime()返回當前Unix時間戳和微秒

這樣你就可以得到然後再執行搜索,然後在操作結束時重新獲取它。然後,您可以簡單地將end_timestart_time相減,然後獲得多長時間的搜索。

// get the time in microseconds before your search 
$start_time = microtime(true); 

/** 
* your code here 
* 
*/ 

// get the time in microseconds when you search is done 
$end_time = microtime(true); 

// finally subtract and print the time elapsed 
echo $start_time - $end_time; 

希望這有助於:-)

+0

值得注意的是(從文檔複製粘貼):_此功能僅在支持'gettimeofday()'系統調用的操作系統上可用._ –