Quick actions

cmd+k|ctrl+k

Navigation

Languages

[function]bench runtime

Snippet info

Language

Php

Visibility

public

Author

mosi

Created

2018-11-21T04:06:32Z

Updated

2018-11-21T04:06:32Z

<?php

/**
 * first call bench() store start time
 * second call bench() get runtime from last time and reset start time
 * @example
 bench();   //start
 for($i = 0; $i < 1e6; $i++) {
    //do something
 }
 echo 'Execute Time: ' . bench();
 */
function bench() {
    static $start;
    if (! $start) {
        $start = microtime(true);
        return;
    }
    $duration = microtime(true) - $start;
    $start = 0;
    return $duration;
}

bench();
echo "Hello \n";
echo 'Execute Time: ' . bench();
INFO