7、利用自定义函数编写程序,计算页面脚本的运行时间。请您利用PHP语言!!!

解决方案 »

  1.   

    function speed() {
      static $t;
      $r = 0;
      if($t) $r = microtime(true) - $t;
      $t = microtime(true);
      return $r;
    }
    speed();
    for($i=0; $i<10000; $i++) {
      for($j=0; $j<1000; $j++);
      $a = $i;
    }
    echo speed();
    php5.4.18实在太快了,循环次数少了根本模拟不出来
      

  2.   

    function speed() {
      static $t; //声明一个静态变量
      $r = 0;
      if($t) $r = microtime(true) - $t; //如果不是第一次进入,则计算秒数差
      $t = microtime(true); //保存当前秒数(精确到微秒)
      return $r;
    }