note 82159 deleted from function.microtime by tularis
| From: | tularis@php.net | Date: | Sun, 22 Jun 2008 12:02:53 +0000 |
| Subject: | note 82159 deleted from function.microtime by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-140657@lists.php.net to get a copy of this message | ||
Note Submitter: blue at ba7rain dot net
----
I write this class for recording and keeping multiple records
with an optional separate function to display a flat number
without the confusing E-005, I have php<5.
<?php
class MicroRecord {
var $record = array();
var $irecord = array();
function MicroRecord(){
$time = $this->GetMicro();
//First one for training :)
$time = $this->GetMicro();
$this->itime = $time;
$this->start = $time;
}
function GetMicro(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function record($name=false,$ini=false){
$now = $this->GetMicro();
$time = $now - $this->start;
$ctime = $now - $this->itime;
if ( ($name === false) || ((string)$name == "" ) ){
$this->record[] = $time;
$this->irecord[] = $ctime;
}
else {
$this->record[(string)$name] = $time;
$this->irecord[(string)$name] = $ctime;
}
$this->start = $now;
if (!$ini) {
return $time;
}
else {
return $ctime;
}
}
function display($name=false,$ini=false){
if ( ($name === false) || ((string)$name == "" ) ){
if (!$ini) {
return $this->record;
}
else {
return $this->irecord;
}
}
else {
if ( isset($this->record[(string)$name]) ){
if (!$ini) {
return $this->record[(string)$name];
}
else {
return $this->irecord[(string)$name];
}
}
}
}
}
function longfloat($n){
$s="/([\-]|[\+])?(\d+)[\.]?(\d+)?[E]([\-]|[\+])(\d+)/i";
if (preg_match($s,$n,$m)){
$m[1] = ( ($m[1] == "-") ? "-" : "" );
$i = strlen($m[2]);
if ( ($m[4] == "-") && ($i > (int)$m[5]) ){
$part1 = substr($m[2].$m[3],0,$i-(int)$m[5]);
$part2 = substr($m[2].$m[3],$i-(int)$m[5]);
$result = $m[1].$part1.".". $part2;
}
else {
$zero = "";
if ($m[4] == "+"){
$j = strlen($m[3]);
$i = (int)$m[5] - ((int)$m[6] - $j);
}
for ($i=$i; $i<(int)$m[5]; $i++) {
$zero .= "0";
}
if ($m[4] == "-"){
$result = $m[1]."0.".$zero.$m[2].$m[3];
}
else {
if ( $j > (int)$m[5] ) {
$part1 = substr($m[3].$zero,0,(int)$m[5]);
$part2 = substr($m[3].$zero,(int)$m[5]);
$result = $m[1].$m[2].$part1.".". $part2;
}
else {
$result = $m[1].$m[2].$m[3].$zero;
}
}
}
}
elseif (preg_match("/([\-]|[\+])?(\d+)[\.]?(\d+)?/i",$n,$m)){
$m[1] = ( ($m[1] == "-") ? "-" : "" );
$result = $m[1].$m[2].( (strlen($m[3]) > 0) ? ".".$m[3] : "" );
}
else {
$result = false;
}
return $result;
}
print "<pre>\r\n";
$micro = new MicroRecord();
//Recording the time for the first process.
print $micro->record();
print "\r\n";
print "\r\n";
//Calculate the second process time starting from the last record.
print $micro->record();
print "\r\n";
print "\r\n";
//Using optional longfloat() function to display long float number as a string.
//Calculate the time from the start/restart of MicroRecord().
//New Record from the start.
print ( longfloat($micro->record('one',true)) );
print "\r\n";
print "\r\n";
//Just Display the record from the start ('one',true).
print ( longfloat($micro->display('one',true)) );
print "\r\n";
print "\r\n";
//Time for this process only ('one').
print ( longfloat($micro->display('one')) );
print "\r\n";
print "\r\n";
//Time record array for each process.
print_r($micro->display());
print "\r\n";
print "\r\n";
//Time record array for the process from the begining of MicroRecord().
print_r($micro->display(false,true));
print "\r\n";
print "\r\n";
//Restart MicroRecord().
$micro->MicroRecord();
print "\r\n";
print "\r\n";
//First process record [0].
print ( longfloat($micro->record(false,true)) );
print "\r\n";
print "\r\n";
//Replacing the first record [0].
print ( longfloat($micro->record(0,true)) );
print "\r\n";
print "</pre>\r\n";
?>