php記錄程式執行時間microtime

來源:互聯網
上載者:User
/** * 記錄程式程式時間---只能記錄連續的調用改函數 * @param$time_key記錄的key,多次執行可以指定同一個key * @returnnone沒有傳回值,直接擷取全域變數$time_arr,如果是在方法中調用需要先global一次 **/function showExecuTime($time_key = '') {global $time_arr,$time_start,$time_end,$time_num;$time_now = microtime(true);if ( !$time_num ) {$time_num = 0;}$time_num++;if ( $time_num%2 == 1 ) {$time_start = $time_now;} else {$time_end = $time_now;}if ( $time_num %2 == 0 && $time_start && $time_end ) {$time = $time_end - $time_start;if ( $time_key ) {$time_arr[$time_key] += $time;} else {$time_arr[] = $time;}$time_arr['total'] += $time;}}/** * 記錄程式程式時間---可以多次交叉的調用 * @param$time_key記錄的key,多次執行可以指定同一個key * @returnnone沒有傳回值,直接擷取全域變數$time_arr,如果是在方法中調用需要先global一次 **/function showExecuTime($time_key = '') {global $time_arr,$time_key_arr,$time_num;$time_now = microtime(true);if ( !$time_key ) {if ( !$time_num ) {$time_num = 0;}$time_num++;$time_key = floor(($time_num+1)/2) - 1;}// 記錄本次是開始時間還是結束時間if ( !isset($time_key_arr[$time_key])) {$time_key_arr[$time_key]['num'] = 0;}$time_key_arr[$time_key]['num']++;if ( $time_key_arr[$time_key]['num']%2 == 1 ) {$time_key_arr[$time_key]['time_start'] = $time_now;} else {$time_key_arr[$time_key]['time_end'] = $time_now;}// 如果連續記錄2次,計算結果if ( $time_key_arr[$time_key]['num'] %2 == 0 && $time_key_arr[$time_key]['time_start'] && $time_key_arr[$time_key]['time_end'] ) {$time = $time_key_arr[$time_key]['time_end'] - $time_key_arr[$time_key]['time_start'];$time_arr[$time_key] += $time;$time_arr['total'] += $time;}}showExecuTime('all');showExecuTime();sleep(1);showExecuTime('all');//////sleep(1);showExecuTime();sleep(1);showExecuTime();sleep(1);showExecuTime();showExecuTime('a');sleep(1);showExecuTime('a');// showExecuTime('all');//////print_r($time_arr);print_r($time_key_arr);

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

以上就介紹了php記錄程式執行時間microtime,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.