php 计算代码执行时间

对于脚本语言的执行时间, 一直都在谈。

PHP的很多的框架实现所谓的bench_mark的来表示自己的执行时间不高之类的。

 

其实对普通的PHP程序也可以看代码的执行时间。

 

 

 

[php] view plaincopy
 
  1. class runtime  
  2. {  
  3.     var $StartTime = 0;  
  4.     var $StopTime = 0;  
  5.    
  6.     function get_microtime()  
  7.     {  
  8.         list($usec, $sec) = explode(' ', microtime());  
  9.         return ((float)$usec + (float)$sec);  
  10.     }  
  11.    
  12.     function start()  
  13.     {  
  14.         $this->StartTime = $this->get_microtime();  
  15.     }  
  16.    
  17.     function stop()  
  18.     {  
  19.         $this->StopTime = $this->get_microtime();  
  20.     }  
  21.    
  22.     function spent()  
  23.     {  
  24.         return round(($this->StopTime - $this->StartTime) * 1000, 1);  
  25.     }  
  26.    
  27. }  

 

 

比如:

 

[php] view plaincopy
 
  1. $run_time = new runtime();  
  2. $run_time->start();  
  3. //begin your code  
  4. //end your code  
  5. $run_time->stop();  
  6. echo "Script running time:".$run_time->spent()."ms";  

 

posted @ 2015-12-11 13:49  Sun丶老板  阅读(316)  评论(0编辑  收藏  举报