测试php代码块执行时间的类

 1 <?php
 2 header("Content-Type: text/html; charset=UTF-8");
 3 class timer {
 4     var $StartTime = 0;
 5     var $StopTime = 0;
 6     var $TimeSpent = 0;
 7 
 8     function start() {
 9         $this -> StartTime = microtime();
10     }
11 
12     function stop() {
13         $this -> StopTime = microtime();
14     }
15 
16     function spent() {
17         if ($this -> TimeSpent) {
18             return $this -> TimeSpent;
19         } else {
20             $StartMicro = substr($this -> StartTime, 0, 10);
21             $StartSecond = substr($this -> StartTime, 11, 10);
22             $StopMicro = substr($this -> StopTime, 0, 10);
23             $StopSecond = substr($this -> StopTime, 11, 10);
24             $start = floatval($StartMicro) + $StartSecond;
25             $stop = floatval($StopMicro) + $StopSecond;
26             $this -> TimeSpent = $stop - $start;
27             return round($this -> TimeSpent, 8) . ‘秒‘;
28         }
29     } // end function spent();
30 
31 }//end class timer;
32 
33 $timer = new timer();
34 $timer -> start();
35 // start ----- 测试代码块
36 $link = mysqli_connect(‘localhost‘, ‘root‘, ‘123456‘, ‘sdk‘, ‘3306‘) or die(‘connect false!‘);
37 $redis = new Redis();
38 $redis -> connect(‘127.0.0.1‘, 6379);
39 $redis -> FLUSHALL();
40 $query = <<<ETO
41 SELECT * FROM `table` AS t1 
42 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`))+(SELECT MIN(id) FROM `table`)) AS id) AS t2 
43 WHERE t1.id >= t2.id ORDER BY t1.id LIMIT 1; 
44 ETO;
45 $result = mysqli_query($link, $query);
46 foreach ($result as $value) {
47     foreach ($value as $key => $val) {
48         $res[$key] = $val;
49     }
50 }
51 $redis -> mset($res);
52 $keys = $redis -> keys(‘*‘);
53 var_dump($keys);
54 echo ‘<br />-- above are redis keys --<br />‘;
55 var_dump($redis -> mget($keys));
56 echo ‘<br />-- above are redis keys of values --<br />‘;
57 // end ----- 测试代码块
58 mysqli_close($link);
59 $timer -> stop();
60 echo ‘</br>运行时间为: ‘ . $timer -> spent();
61 unset($timer);

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。