measuring the elapsed time between code segments in PHP

后端 未结 7 1749
傲寒
傲寒 2020-12-15 19:32

From time time to time, I\'d like to be able to measure the elapsed time between two segments of code. This is solely to be able to detect the bottlenecks within the code an

相关标签:
7条回答
  • 2020-12-15 20:11

    Other factors affect the timing of your scripts. Example:

    1. Complex code and recursive functions.
    2. The type of web server being used, example: shared VS dedicated hosting.
    0 讨论(0)
  • 2020-12-15 20:12
    <?php
    $time_start = microtime(true);
    
    // Sleep for a while (or your code which you want to measure)
    usleep(100);
    
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    
    echo "Did nothing in $time seconds\n";
    

    Source: http://php.net/manual/en/function.microtime.php#example-2568

    0 讨论(0)
  • 2020-12-15 20:12

    Something along these lines should work:

    $start = microtime(true); 
    
    // Do something
    sleep(2);
    
    $end = (microtime(true) - $start);
    echo "elapsed time: $end";
    
    0 讨论(0)
  • 2020-12-15 20:21

    My profiling needs in development are covered by this small yet powerful class:

    <?php
    
    class perflog {
        protected $perflog = [];
    
        public function start($tag) {
            if (!isset($this->perflog[$tag])) $this->perflog[$tag] = 0;
            $this->perflog[$tag] -= microtime(TRUE);
        }
    
        public function stop($tag) {
            $this->perflog[$tag] += microtime(TRUE);
        }
    
        public function results() {
            return $this->perflog;
        }
    }
    

    See it in action here.

    It is intended to be invoked via subsequent start(<tag>) and stop(<tag>) calls. It produces an array with the totalized times your code spent in the sections enclosed by the calls of start() and stop() with matching tags.

    start-stop sequences may be nested and may be entered multiple times, thus summarizing the time spent in the enclosed section.

    Its compactness ensures minimum performance impact. Dynamic tag creation can be used to let the program modify what it monitors. Typically this is extended with outputting or storing functions.

    0 讨论(0)
  • 2020-12-15 20:24

    From the first example in the php docs:

    <?php
    /**
     * Simple function to replicate PHP 5 behaviour
     */
    function microtime_float()
    {
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }
    
    $time_start = microtime_float();
    
    // Sleep for a while
    usleep(100);
    
    $time_end = microtime_float();
    $time = $time_end - $time_start;
    
    echo "Did nothing in $time seconds\n";
    
    0 讨论(0)
  • 2020-12-15 20:28

    Same drew010 function (thanks!), only added custom comment and time displays in microseconds (us):

        function time_elapsed($comment) 
            {
    
            static $time_elapsed_last = null;
            static $time_elapsed_start = null;
    
            // $unit="s"; $scale=1000000; // output in seconds
            // $unit="ms"; $scale=1000; // output in milliseconds
            $unit="μs"; $scale=1; // output in microseconds
    
            $now = microtime(true);
    
            if ($time_elapsed_last != null) {
                echo "\n";
                echo '<!-- ';
                echo "$comment: Time elapsed: ";
                echo round(($now - $time_elapsed_last)*1000000)/$scale;
                echo " $unit, total time: ";
                echo round(($now - $time_elapsed_start)*1000000)/$scale;
                echo " $unit -->";
                echo "\n";
            } else {
                $time_elapsed_start=$now;
            }
    
            $time_elapsed_last = $now;
        }               
    

    Example:

    // Start timer
    time_elapsed('');
    
    // Do something
    usleep(100);
    time_elapsed('Now awake, sleep again');
    
    // Do something
    usleep(100);
    time_elapsed('Game over');
    

    Ouput:

    <!-- Now awake, sleep again: Time elapsed: 100 us, total time: 100 us -->
    <!-- Game over: Time elapsed: 100 us, total time: 200 us -->
    
    0 讨论(0)
提交回复
热议问题