首页 > 语言 > PHP > 正文

php计算函数执行时间的方法

2024-09-04 11:42:48
字体:
来源:转载
供稿:网友

这篇文章主要介绍了php计算函数执行时间的方法,以md5函数加密运行时间为例分析了php计算函数运行时间的技巧,需要的朋友可以参考下.

本文实例讲述了php计算函数执行时间的方法,分享给大家供大家参考,具体如下:

我们可以通过在程序的前后分别记录开始和结束时间,两个时间差就是程序的执行时间。

  1. <?php 
  2. $long_str = "this is a test to see how much time md5 function takes to execute over this string"
  3. // start timing from here 
  4. $start = microtime(true); 
  5. // function to test 
  6. $md5 = md5($long_str); 
  7. $elapsed = microtime(true) - $start
  8. echo "That took $elapsed seconds./n"
  9. ?> 

运行结果如下:

That took 7.1525573730469E-6 seconds.

php 计算函数执行时间的方法及获得微妙的方法

  1. // 获得微妙方法 
  2.  function getMillisecond() 
  3.  { 
  4.    list($s1$s2) = explode(' ', microtime()); 
  5.    return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); 
  6.  } 

原理:分别记录函数开始时间和结束时间,然后时间差就是函数执行的时间

  1. <?php 
  2.  $start_time = microtime(true); 
  3. for($i=1;$i&lt;=1000;$i++){ 
  4. echo $i.'&lt;br&gt;'
  5. $end_time = microtime(true); 
  6. echo '循环执行时间为:'.($end_time-$start_time).' s'
  7. ?>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表