PHP kod yürütme zamanı

7 Cevap

Benim kod yürütme süresini nasıl bulabilirim?

Ben aşağıda parçacığını kullanıyorum.

Ben mutlu değilim?

list ($msec, $sec) = explode(' ', microtime());
$microtime = (float)$msec + (float)$sec;

7 Cevap

Bunun için bu basit bir sınıf oluşturduk:

class timer
{
    private $start_time = NULL;
    private $end_time = NULL;

    private function getmicrotime()
    {
      list($usec, $sec) = explode(" ", microtime());
      return ((float)$usec + (float)$sec);
    }

    function start()
    {
      $this->start_time = $this->getmicrotime();
    }

    function stop()
    {
      $this->end_time = $this->getmicrotime();
    }

    function result()
    {
        if (is_null($this->start_time))
        {
            exit('Timer: start method not called !');
            return false;
        }
        else if (is_null($this->end_time))
        {
            exit('Timer: stop method not called !');
            return false;
        }

        return round(($this->end_time - $this->start_time), 4);
    }

    # an alias of result function
    function time()
    {
        $this->result();
    }

}

Zaman komut için, sizin gibi kullanmak için koyabilirsiniz:

$timer = new timer();

$timer->start();
// your code now
$timer->stop();

// show result now
echo $timer->result();

Ben geçen süreyi belirlemek için aşağıdaki sınıf kullanıyorum:

class StopWatch {
    private static $total;

    public static function start() {
        self::$total = microtime(true);
    }

    public static function elapsed() {
        return microtime(true) - self::$total;
    }
}

Eğer baştan geçen ne kadar zaman bilmek istediğiniz zaman sadece başında StopWatch::start çağırır ve StopWatch::elapsed var.

Sen kullanabilirsiniz parameter of microtime:

$microtime = microtime(true);

Eğer komut profile istiyorsanız, XDebug + {K, Win} Cachegrind, bu senin kodun yürütülmesine görsel bir haritasını sağlayan ücretsiz araçlardır kullanın.

Eğer gerçek işlemci zamanlamalarını isterseniz:

$rUsage = getrusage();
echo 'User time = '.sprintf('%.4f',($rUsage['ru_utime.tv_sec'] * 1e6 + $rUsage['ru_utime.tv_usec']) / 1e6).' seconds';
echo 'system time = '.sprintf('%.4f',($rUsage['ru_stime.tv_sec'] * 1e6 + $rUsage['ru_stime.tv_usec']) / 1e6).' seconds';

Bilgi paylaşımı için teşekkürler.

Benim çözüm yakın aşağıda bir de BIRAZ.

function microtime_float() {
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$time_start = microtime_float();

SOURCE CODE

$time_end = microtime_float();
$time = round($time_end - $time_start, 4);

echo "Last uncached content render took $time seconds";

Unix çalıştırılıyorsa, bakmak time command: