Class code for calculating program running time in php

高洛峰
Release: 2023-03-02 18:14:01
Original
1268 people have browsed it

Copy the code The code is as follows:
class Timer {
private $StartTime = 0;//The time the program starts running
private $StopTime = 0;//The time the program ends
private $TimeSpent = 0;//The time it takes to run the program
function start(){//The program starts running
$this->StartTime = microtime();
}
function stop(){//The program ends
$this->StopTime = microtime();
}
function spent(){//The time it takes for the program to run
if ($this->TimeSpent) {
return $this->TimeSpent;
} else {
list($StartMicro, $StartSecond) = explode(" " , $this->StartTime);
list($StopMicro, $StopSecond) = explode(" ", $this->StopTime);
$start = doubleval($StartMicro) + $StartSecond;
$stop = doubleval ($StopMicro) + $StopSecond;
$this->TimeSpent = $stop - $start;
return substr($this->TimeSpent,0,8)."seconds";//Return the obtained program execution Time difference
}
}
}
$timer = new Timer();
$timer->start();
//...Code for program running
$timer->stop();
echo "program The running time is: ".$timer->spent();

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template