這篇文章主要介紹了php計算函數執行時間的方法,以md5函數加密運行時間為例分析了php計算函數運行時間的技巧,需要的朋友可以參考下.
本文實例講述了php計算函數執行時間的方法,分享給大家供大家參考,具體如下:
我們可以通過在程序的前后分別記錄開始和結束時間,兩個時間差就是程序的執行時間。
- <?php
- $long_str = "this is a test to see how much time md5 function takes to execute over this string";
- // start timing from here
- $start = microtime(true);
- // function to test
- $md5 = md5($long_str);
- $elapsed = microtime(true) - $start;
- echo "That took $elapsed seconds./n";
- ?>
運行結果如下:
That took 7.1525573730469E-6 seconds.
php 計算函數執行時間的方法及獲得微妙的方法
- // 獲得微妙方法
- function getMillisecond()
- {
- list($s1, $s2) = explode(' ', microtime());
- return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
- }
原理:分別記錄函數開始時間和結束時間,然后時間差就是函數執行的時間
- <?php
- $start_time = microtime(true);
- for($i=1;$i<=1000;$i++){
- echo $i.'<br>';
- }
- $end_time = microtime(true);
- echo '循環執行時間為:'.($end_time-$start_time).' s';
- ?>
新聞熱點
疑難解答