php获取页面执行时间
1 <?php 2 3 class runtime 4 { 5 6 7 function __construct() { 8 $db_config = array( 9 'host' => '127.0.0.1', 10 'port' => 3306, 11 'user' => 'root', 12 'pwd' => 'root', 13 'dbname' => 'test' 14 ); 15 $link = mysql_connect($db_config['host'], $db_config['user'], $db_config['pwd']); 16 if (!$link) { 17 die('Could not connect: ' . mysql_error()); 18 } 19 mysql_query("set names 'utf8'"); 20 mysql_select_db($db_config['dbname'], $link) or die('Can\'t use foo : ' . mysql_error()); 21 } 22 23 var $StartTime = 0; 24 var $StopTime = 0; 25 function get_microtime() 26 { 27 list($usec, $sec) = explode(' ', microtime()); 28 return ((float)$usec + (float)$sec); 29 } 30 function start() 31 { 32 $this->StartTime = $this->get_microtime(); 33 } 34 function stop() 35 { 36 $this->StopTime = $this->get_microtime(); 37 } 38 function spent() 39 { 40 $total = ($this->StartTime-$this->StopTime); 41 return round($total * 1000, 1); 42 } 43 } 44 45 46 $runtime= new runtime; 47 $runtime->start();//计时开始//中间放置需要计时的函数或过程 48 //for($i=1;$i<=1000;$i++) 49 //{ 50 // $id="100"; 51 // $username="xm"; 52 // //$this->conn = new Mysql($db_config); 53 // //$sql="INSERT INTO tb_user(id,name) values('$id','$username')"; 54 // //mysql_query($sql); 55 //} 56 for ($i=1; $i<=1000;$i++) { 57 $id = '100'; 58 $username = 'xm'; 59 $sql[] = '('.$id.',"'.$username.'")'; 60 } 61 mysql_query('INSERT INTO tb_user(id,name) values '.implode(',',$sql)); 62 $runtime->stop();//计时结束 63 echo "页面执行时间:".$runtime->spent()."毫秒";