PHP Compute script Execution Time class PHP script parameters php Script execute php script how to ship

Source: Internet
Author: User
1. When optimizing the code, the execution time of the script is a very important consideration, so how to use PHP to calculate the running time of PHP script?

The following recommended to everyone a very useful class.

runtime.class.php

/**
* PHP Script Execution Time calculation
*/
Class runtime
{
var $StartTime = 0;
var $StopTime = 0;
function Get_microtime ()
{
List ($usec, $sec) = explode (' ', Microtime ());
return (float) $usec + (float) $sec);
}
function Start ()
{
$this->starttime = $this->get_microtime ();
}
function Stop ()
{
$this->stoptime = $this->get_microtime ();
}
Function spent ($echo =false, $title = ")
{
$spent = sprintf ('%.4f ', round (($this->stoptime-$this->starttime) * 1000, 1)/1000);
if ($echo) {
echo $title. " Execution time: {$spent} seconds
";
}else{
return $spent;
}
}
function Clear ()
{
$this->starttime = 0;
$this->stoptime = 0;
}
}

Test code:

#测试脚本代码
$runtime = new Runtime;
$runtime->start ();
$a = 0;
for ($i =0; $i <100000; $i + +)
{
$a *= $i;
}
$runtime->stop ();
$spent _time = $runtime->spent ($echo =true, ' test script ');
$runtime->clear ();

Test results:


The above describes the PHP calculation script execution time class, including PHP, scripting content, want to be interested in PHP tutorial friends helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.