2012-01-18 102 views
1

前幾天寫了一個php腳本,通過我所有的音樂,並讀取每首歌曲的id3標籤,並將它們插入到mysql數據庫中。這裏有一個背景片段:如何計算執行時間(php指令'max_execution_time'?)

exec ("find /songs -type f -iname '*mp3'", $song_path); 
$number_of_songs = count($song_path); 
for($i=0; $i<$number_of_songs; $i++){ 
    //read id3 tags 
    //store id3 tags into database 
} 

我的Apache2/php.ini中改變了PHP指令max_execution_time更好地理解這個指令做什麼。

當我設置max_execution_time = 10時,我的php腳本運行約45秒,併成功讀取約150首歌曲(超過數千首歌曲)的id3標記,並在終止腳本並輸出以下內容之前將這些標記插入到mysql數據庫中到屏幕上:

致命錯誤:在/websites/.../public_html/GetID3()/getid3/module.audio.mp3.php超過上線10秒最大執行時間1894

從文檔中,'最大執行時間不受系統調用,流操作等影響'http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time

  1. 我可以從 maximum_execution_time之間的差異推斷出什麼被設置爲10秒和腳本 運行總共45秒前結束?這個 意味着45秒內,35花了非PHP相關的 活動,如讀取ID3標籤,插入數據到MySQL等...,而花10年做 做PHP的相關活動,如迭代循環?

  2. 有沒有一種方法可以計算執行時間並將其打印到 的屏幕?

EDIT 使用定時器袞建議,我稱爲在循環結束時的getTime()函數,大約有超過100個迭代循環。下面是輸出到我的瀏覽器:

0.1163秒

0.8142秒

1.1379秒

1.5555秒

...

76.7847秒

77.2008秒

77.6071秒

致命錯誤:在/websites/.../public_html/GetID3()/getid3/module.audio.mp3超過了10秒最大執行時間。PHP的在線505

+0

你碰巧在使用set_time_limit(http://php.net/set_time_limit)嗎? '被調用時,set_time_limit()從零開始重新啓動超時計數器。換句話說,如果超時時間是默認的30秒,並且腳本執行時間爲25秒,則會執行一次諸如set_time_limit(20)的調用,腳本將在超時之前運行總共45秒。 – Corbin 2012-01-18 03:31:08

+0

不,我不使用set_time_limit – user784637 2012-01-18 04:05:58

回答

1

我不相信腳本actully爲超過10秒運行,你需要把正確的計時器在它

<!-- put this at the top of the page --> 
<?php 
function getTime() { 
    $timer = explode(' ', microtime()); 
    $timer = $timer[1] + $timer[0]; 
    return $timer; 
} 

$start = getTime(); 
?> 

<!-- put other code and html in here --> 

<!-- put this code at the bottom of the page --> 
<?php 
$end = getTime(); 
$time_took= round($end - $start,4).' seconds'; 
echo $time_took; 
?> 
+0

感謝Dagon請看我的編輯在我原來的帖子 – user784637 2012-01-18 05:09:21

1

這種類型的腳本應該真正在執行CLI環境,而不是由您的Web服務器執行的php進程。由於每manual docs對PHP命令行環境與其他PHP SAPIs的區別:

PHP in a shell environment tends to be used for a much more diverse range of purposes than typical Web-based scripts, and as these can be very long-running, the maximum execution time is set to unlimited.

雖然它不回答你的問題,它確實解決您的問題:)

+0

嘿,感謝的人。我通過cli處理腳本,但是在這種情況下,我想創建一個用戶可以登錄/註銷的網站,以便我可以解析他們的id3標籤,這就是爲什麼我要從cgi進行測試的原因。 – user784637 2012-01-18 04:04:50

+0

順便說一句,你類似於亞當萊文:栗色的歌手5 – user784637 2012-01-18 04:05:28

+0

@LedZeppelin哈哈,當我住在洛杉磯時,我常常在深夜喝醉女孩說同樣的話。至於你的網站用例......你可能會更好地產生一個新的進程來處理標籤解析。這樣你仍然可以利用「CLI」環境。此外,對於這種類型的處理工作,您應該考慮使用某種排隊系統,因爲像這樣的長處理作業不適合單個Web服務器php進程。 – rdlowrey 2012-01-18 05:00:48

1

好像你不僅嘗試來衡量劇本的持續時間,但也試圖限制它。而在你的情況下,max_execution_time不是你想要的。

基本上,「最大執行時間不受系統調用,流操作等影響」是正確的。如果您需要限制實時腳本長度,則需要實施自己的時間測量。人們通常爲它編寫一些基準測試類,這畢竟將有助於優化腳本,但簡單

$timer['start'] = time(); 
$timer['max_exec_time'] = 10; // seconds 

開始和

if (time() > $timer['start'] + $timer['max_exec_time']) 
    break; // or exit; etc 

在循環的末尾或其他地方你想應該夠了。

3
<!-- Paste this at the top of the page --> 
<?php 
    $mic_time = microtime(); 
    $mic_time = explode(" ",$mic_time); 
    $mic_time = $mic_time[1] + $mic_time[0]; 
    $start_time = $mic_time; 
?> 

<!-- Write Your script(executable code) here --> 

enter code here 

<!-- Paste this code at the bottom of the page --> 
<?php 
    $mic_time = microtime(); 
    $mic_time = explode(" ",$mic_time); 
    $mic_time = $mic_time[1] + $mic_time[0]; 
    $endtime = $mic_time; 
    $total_execution_time = ($endtime - $start_time); 
    echo "Total Executaion Time ".$total_execution_time." seconds"; 
?>