ホームページ > バックエンド開発 > PHPチュートリアル > phpタイマーページの実行時間監視クラス

phpタイマーページの実行時間監視クラス

黄舟
リリース: 2023-03-05 19:32:01
オリジナル
1384 人が閲覧しました

php 異なるキーに従って異なる実行時間を監視できるタイマーページの実行時間監視クラス


Timer.class.php


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

<?php

/** Timer class, 计算页面运行时间,可按不同key计算不同的运行时间

*   Date:   2014-02-28

*   Author: fdipzone

*   Ver:    1.0

*

*   Func:

*   public  start        记录开始时间

*   public  end          记录结束时间

*   public  getTime      计算运行时间

*   pulbic  printTime    输出运行时间

*   private getKey       获取key

*   private getMicrotime 获取microtime

*/

 

class Timer{ // class start

 

    private $_start = array();

    private $_end = array();

    private $_default_key = &#39;Timer&#39;;

    private $_prefix = &#39;Timer_&#39;;

 

 

    /** 记录开始时间

    * @param String $key 标记

    */

    public function start($key=&#39;&#39;){

        $flag = $this->getKey($key);

        $this->_start[$flag] = $this->getMicrotime();

    }

 

 

    /** 记录结束时间

    * @param String $key 标记

    */

    public function end($key=&#39;&#39;){

        $flag = $this->getKey($key);

        $this->_end[$flag] = $this->getMicrotime();

    }

 

 

    /** 计算运行时间

    * @param  String $key 标记

    * @return float

    */

    public function getTime($key=&#39;&#39;){

        $flag = $this->getKey($key);

        if(isset($this->_end[$flag]) && isset($this->_start[$flag])){

            return (float)($this->_end[$flag] - $this->_start[$flag]);

        }else{

            return 0;

        }

    }

 

 

    /** 输出页面运行时间

    * @param  String $key 标记

    * @return String

    */

    public function printTime($key=&#39;&#39;){

        printf("%srun time %f ms\r\n", $key==&#39;&#39;? $key : $key.&#39; &#39;, $this->getTime($key)*1000);

    }

 

 

    /** 获取key

    * @param  String $key 标记

    * @return String

    */

    private function getKey($key=&#39;&#39;){

        if($key==&#39;&#39;){

            return $this->_default_key;

        }else{

            return $this->_prefix.$key;

        }

    }

 

 

    /** 获取microtime

    */

    private function getMicrotime(){

        list($usec, $sec) = explode(&#39; &#39;, microtime());

        return (float)$usec + (float)$sec;

    }

 

 

} // class end

 

?>

ログイン後にコピー

デモ:



1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<?php

 

require &#39;Timer.class.php&#39;;

 

$timer = new Timer();

$timer->start();

 

$timer->start(&#39;program1&#39;);

usleep(mt_rand(100000,500000));

$timer->end(&#39;program1&#39;);

$timer->printTime(&#39;program1&#39;);

 

$timer->start(&#39;program2&#39;);

usleep(mt_rand(100000,500000));

$timer->end(&#39;program2&#39;);

$timer->printTime(&#39;program2&#39;);

 

$timer->end();

$timer->printTime();

 

?>

ログイン後にコピー

デモ実行出力:

上記はphpタイマーページの実行時間監視の内容ですclass, more 関連コンテンツの詳細については、PHP 中国語 Web サイト (www.php.cn) に注目してください。


関連ラベル:
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート