Archive

Posts Tagged ‘script timer’

Script Timing in PHP

August 4th, 2008
Comments Off

Knowing how long something takes to process is very important. However, hand-rolling a timing solution every time you need one can be frustrating. This little class is a very lightweight solution that allows you to to simply start a timer, then capture elapsed time at various points in your program.

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
89
90
91
92
93
94
95
96
97
98
 
class Script_Timer
{
    /**
     * Start timer
     *
     * @return BOOLEAN
    */

    public static function start()
    {
        $success = false;
        try
        {
            if(!self::started())
            {
                define('TIMER_START_TIME', microtime());
            }

            $success = true;
        }
        catch(Exception $e)
        {
            //
        }

        return $success;
    }

    /**
     * Check if timer is started
     *
     * @return  BOOLEAN status of timer
     */

    public static function started()
    {
        $started = false;

        if(defined('TIMER_START_TIME'))
        {
            $started = true;
        }

        return $started;
    }

    /**
     * Get timestamp of start time
     *
     * @return STRING   microtime of time start
     */

    public static function get_Start()
    {
        $start_time = null;

        if(self::started())
        {
            $start_time = TIMER_START_TIME;
        }

        return $start_time;
    }

    /**
     * Get number of seconds since script start
     *
     * @param INTEGER   $decimals   number of decimals to capture
     *
     * @return NUMBER   seconds since script start
    */

    public static function get($decimals=2)
    {
        // format start time
        $start_time = explode(' ', TIMER_START_TIME);
        $start_time = $start_time[1] + $start_time[0];
        // get and format end time
        $end_time = explode(' ', microtime());
        $end_time = $end_time[1] + $end_time[0];

        return number_format($end_time - $start_time, $decimals);
    }

    /**
     * Get number of seconds from given count
     *
     * @param NUMBER    $prev_seconds   number of seconds from a previous get() value
     * @param INTEGER   $decimals       number of decimals to capture
     *
     * @return NUMBER   seconds since script start
     */

    public static function diff($prev_seconds, $decimals=2)
    {
        $current_time = self::get(4);

        $diff_seconds = $current_time - $prev_seconds;

        return number_format($diff_seconds, $decimals);
    }
}

The example below shows how the class is used statically so that the scope of an instantiated timer object doesn’t become a problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// start timer  Script_Timer::start();

// do some processing.....  

// Then when you are ready to get current time since start, run:

$new_time_1 = Script_Timer::get();

// do some processing.....

$new_time_2 = Script_Timer::get();

// You can also get the difference from another time until current:

// do some processing.....

$time_diff = Script::diff($new_time_2);

You can even set the timed values to an arrar or session, so that you can keep them together and run evalutions and comparisons on them later.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// start timer
Script_Timer::start();

$_SESSION['timer'] = array();

// initiate first time capture

$_SESSION['timer']['start'] = Script_Timer::get();

// do some processing.....

$_SESSION['timer']['point_a'] = Script_Timer::get();   

// do some processing.....

$_SESSION['timer']['point_b'] = Script_Timer::get();

// do some processing.....

$_SESSION['timer']['point_c'] = Script_Timer::get();

$point_c_processing_time = $_SESSION['timer']['point_c'] - $_SESSION['timer']['point_b'];

Using the time statically, as shown above, can be great for an entire application, however, sometimes you just want to implement a timer within a specific class. In this case, you might consider using the the timer for internal class use only.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class My_Class
{
    public function __construct()
    {
        Script_Timer::start();
    }

    public function my_Method()
    {
        // do something
 
       echo Script_Timer::get() . ' seconds';
    }
}

Development, PHP, Uncategorized , , ,



Sponsored Links



agile ajax black hat Cake PHP centering clifford stoll css cuckoo's egg energy energy drinks espionage flash Flex hacker jquery modular MVC objects optimization performance PHP script timer smarty smarty templates stylesheet up-time uptime variable scope web 2.0 Zend Framework